Get the visitor IP Address

The following snippet shows how to retrieve the IP Address of visitor of your website. Usually, the Request object contains the IP Address in the HTTP_X_FORWARDED_FOR key, but sometimes the ISP provider overrides it to make it null. In that case, you can use REMOTE_ADDR.


//Get the client IP Address
string LstrIPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(LstrIPAddress))
{
	LstrIPAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}