关于web请求中 获取真实IP

HttpRequest request = HttpContext.Current.Request;

                if (!string.IsNullOrEmpty(request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
                {
                    //ip = GetCDNIP(request.ServerVariables["HTTP_ARR_IP"]);
                    if (string.IsNullOrEmpty(ip))
                    {
                        ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
                    }
                }
                if (string.IsNullOrEmpty(ip) || ip.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length != 4)
                {
                    ip = GetArrIP();
                    if (!string.IsNullOrWhiteSpace(ip) && ip.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length != 4)
                    {
                        ip = string.Empty;
                    }
                }
                //if (string.IsNullOrEmpty(ip))
                //{
                //    ip = GetCDNIP(null);
                //}
                if (string.IsNullOrEmpty(ip))
                {
                    ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }

                if (string.IsNullOrEmpty(ip))
                {
                    ip = request.UserHostAddress;
                }

  

原文地址:https://www.cnblogs.com/Darker-Samuel/p/5870746.html