I want to know the IP address of the client machine, i.e. the IP address of the user who is browsing my website. I am trying the following code but it is returning server address -
public string GetClientIP()
{
string result = string.Empty;
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ip))
{
string[] ipRange = ip.Split(',');
int le = ipRange.Length - 1;
result = ipRange[0];
}
else
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
return result;
}
How can I find the right IP address?
You can use "HTTP_X_FORWARDED_FOR" or "REMOTE_ADDR" header attribute.
Refer method GetVisitorIPAddress from developersnote.com blog .
Note: For getting the public ip address this is used.
Client IP can be read from request:
Here is code for getting more than just client IP address: