WebHelper
/// <summary> /// 获取web客户端ip /// </summary> /// <returns></returns> public static string GetWebClientIp() { try { if (HttpContext.Current == null || HttpContext.Current.Request == null || HttpContext.Current.Request.ServerVariables == null) return ""; string CustomerIP = ""; //CDN加速后取到的IP CustomerIP = HttpContext.Current.Request.Headers["Cdn-Src-Ip"]; if (!string.IsNullOrEmpty(CustomerIP)) { return CustomerIP; } if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null) { CustomerIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (CustomerIP == null) CustomerIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } else { CustomerIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } if (string.Compare(CustomerIP, "unknown", true) == 0) return HttpContext.Current.Request.UserHostAddress; return CustomerIP; /** lcq 关键就在HTTP_X_FORWARDED_FOR 使用不同种类代理服务器,上面的信息会有所不同: 一、没有使用代理服务器的情况: REMOTE_ADDR = 您的 IP HTTP_VIA = 没数值或不显示 HTTP_X_FORWARDED_FOR = 没数值或不显示 二、使用透明代理服务器的情况:Transparent Proxies REMOTE_ADDR = 代理服务器 IP HTTP_VIA = 代理服务器 IP HTTP_X_FORWARDED_FOR = 您的真实 IP 这类代理服务器还是将您的信息转发给您的访问对象,无法达到隐藏真实身份的目的。 三、使用普通匿名代理服务器的情况:Anonymous Proxies REMOTE_ADDR = 代理服务器 IP HTTP_VIA = 代理服务器 IP HTTP_X_FORWARDED_FOR = 代理服务器 IP 隐藏了您的真实IP,但是向访问对象透露了您是使用代理服务器访问他们的。 四、使用欺骗性代理服务器的情况:Distorting Proxies REMOTE_ADDR = 代理服务器 IP HTTP_VIA = 代理服务器 IP HTTP_X_FORWARDED_FOR = 随机的 IP 告诉了访问对象您使用了代理服务器,但编造了一个虚假的随机IP代替您的真实IP欺骗它。 五、使用高匿名代理服务器的情况:High Anonymity Proxies (Elite proxies) REMOTE_ADDR = 代理服务器 IP HTTP_VIA = 没数值或不显示 HTTP_X_FORWARDED_FOR = 没数值或不显示 **/ } catch (Exception) { return string.Empty; } } /// <summary> /// 获取服务器IP /// </summary> /// <returns></returns> public static string GetLocalIp() { try { var ipHostEntry = Dns.GetHostEntry(Dns.GetHostName()); var address = ipHostEntry.AddressList.FirstOrDefault(i => i.AddressFamily == AddressFamily.InterNetwork); var localIpAddress = address == null ? string.Empty : address.ToString(); return localIpAddress; } catch (Exception) { return string.Empty; } }