欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 获取客户端IP

程序员文章站 2022-12-25 12:09:16
/// /// 获得当前页面客户端的IP /// /// 当前页面客户端的IP public static string GetIP() { try { string result = String.Empty; ... ......
        /// <summary>
        /// 获得当前页面客户端的ip
        /// </summary>
        /// <returns>当前页面客户端的ip</returns>
        public static string getip()
        {
            try
            {
                string result = string.empty;

                result = httpcontext.current.request.servervariables["http_x_forwarded_for"];
                if (string.isnullorempty(result))
                {
                    result = httpcontext.current.request.servervariables["remote_addr"];
                }

                if (string.isnullorempty(result))
                {
                    result = httpcontext.current.request.userhostaddress;
                }

                if (string.isnullorempty(result) || !utils.isip(result))
                {
                    return "127.0.0.1";
                }

                return result;
            }
            catch (exception ex)
            {
                system.diagnostics.trace.write(ex.message);
                return httpcontext.current.request.userhostaddress;
            }
        }