public string userip()
{
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
else
{
ip = ip.ToString().Split(',')[0].Trim();
if (!IsIP(ip))
ip = null;
}
if (string.IsNullOrEmpty(ip))
ip = HttpContext.Current.Request.UserHostAddress;
if (string.IsNullOrEmpty(ip))
ip = "0.0.0.0";
return ip;
}到底如何才能获取真实的客户端IP地址?
网上的都是不正确的获取方式
目前普通的是判断HTTP_X_FORWARDED_FOR,有则取代理IP,为null则取REMOTE_ADDR
而实现的情况是:
客户端如果使用了代理,则通过HTTP_X_FORWARDED_FOR可以判断后获取
客户端如果使用了代理,并伪造了X_FORWARDED_FOR头,则判断X_FORWARDED_FOR获取的是假的,IIS日志记录下的代理IP
{
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
else
{
ip = ip.ToString().Split(',')[0].Trim();
if (!IsIP(ip))
ip = null;
}
if (string.IsNullOrEmpty(ip))
ip = HttpContext.Current.Request.UserHostAddress;
if (string.IsNullOrEmpty(ip))
ip = "0.0.0.0";
return ip;
}到底如何才能获取真实的客户端IP地址?
网上的都是不正确的获取方式
目前普通的是判断HTTP_X_FORWARDED_FOR,有则取代理IP,为null则取REMOTE_ADDR
而实现的情况是:
客户端如果使用了代理,则通过HTTP_X_FORWARDED_FOR可以判断后获取
客户端如果使用了代理,并伪造了X_FORWARDED_FOR头,则判断X_FORWARDED_FOR获取的是假的,IIS日志记录下的代理IP
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货