获取IP地址

     /// <summary>
        /// 获得客户端IP地址,如果客户端使用代理服务器,并且代理服务器在HTTP Header中包含相关信息,则将返回用户真实IP地址
        /// </summary>
        public static string IP
        {
            get
            {
                string ipStr = "";
                try
                {
                    if (HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"] != null)
                    {
                        ipStr = HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"].ToString();
                    }
                    else if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                    {
                        ipStr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                    }
                    else
                    {
                        ipStr = HttpContext.Current.Request.UserHostAddress;
                    }
                }
                catch { }
                return ipStr;
            }
        }
原文地址:https://www.cnblogs.com/zy-theone/p/9885468.html