//方法一
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);