//方法一
HttpContext.Current.Request.UserHostAddress;
//方法二
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
//方法三
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
//方法四(无视代理)
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
如果没有使用代理IP,那么使用无视代理的方法则不能获取到IP地址,这样判断一下就可以获取到最后的IP了
string userip= HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (userip==null)
{
userip = HttpContext.Current.Request.UserHostAddress;
}
Response.Write(userip);