
服務端方法:
//方法一

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"];
用戶端方法:

//方法五

var ip = '<!--#echo var="REMOTE_ADDR"-->';

alert("Your IP address is "+ip);


//方法六(無視代理)

function GetLocalIPAddress()
{
var obj = null;
var rslt = "";
try
{
obj = new ActiveXObject("rcbdyctl.Setting");
rslt = obj.GetIPAddress;
obj = null;
}
catch(e)
{
//
}
return rslt;
}
-----------------------------------------------------------------------------------------
來自印度的MCT Maulik Patel提供了一種服務端的解決方案,很好:
if (Context.Request.ServerVariables[ " HTTP_VIA " ] != null ) // 伺服器, using proxy
{ 得到真實的用戶端位址
ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
}
else//如果沒有使用代理伺服器或者得不到用戶端的ip not using proxy or can't get the Client IP
{ 得到服務端的位址
ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
}
備注:
1. 有些代理是不會發給我們真實IP位址的
2. 有些用戶端會因為“header_access deny”的安全設定而不發給我們IP
文章來源:http://blog.sina.com.cn/s/blog_621253a60100lp4e.html