Thursday, May 14, 2009

Get Remote Client's IP Address and UserName.

In asp.net we can get IP Address of WebClient using Request object.and If User is Loggen in With Some Authentication like Forms,Windows or any other. we can also track detail of User Loggen in User on that Web Site.

string strIpAddress = string.Empty;
strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIpAddress == null)
{
strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
}


In above Code strIpAddress will Contain IP of Client machine..
this will not show up real IP if your Client is Behind LAN firewalls.

Request.ServerVariables["REMOTE_USER"]

the code in above line will returns UserName . If User is Loggen in..

Thanks.