private string GetIp()
{
string ip = "";
if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
{
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(new char[] {','})[0];
// Return real client IP.
}
if (string.IsNullOrEmpty(ip))
{
ip = Context.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
return ip;
}
{
string ip = "";
if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
{
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(new char[] {','})[0];
// Return real client IP.
}
if (string.IsNullOrEmpty(ip))
{
ip = Context.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
return ip;
}
不能排除在使用欺骗代理和伪造ip情况下获取不到真实Ip