Sunday, November 23, 2008

Machine ip of client machine using asp.net

Firstly, Request.ServerVariables["HTTP_X_FORWARDED_FOR"] is a correct way to retrieve client IP address. The below code is an approach for getting Clinet IP.

if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // using proxy
{
ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
}
else// 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.
}

You can not always get the Client IP through a proxy.

1. Some proxys can not send the client IP to you.
2. Some Clients can prevent from sending IP by "header_access deny".