Get Browser’s IP Address Using ServerVariables

Posted on: June 30th, 2011 by
Comments Requested

Knowing the client’s IP Address is a major benefit for web applications especially when the application has users whom post information and content into the site. Tracking user activity or honing in on the source of harmful activity is the start to improving our services and solving issues. Further, we might also want to map a client’s IP address to a specific member account, honing in on relevance even before the user logs in. Here’s a quick way to grab a browser’s IP address in C# using ServerVariables:

public string GetIP()
{
string ipaddress = “”;

//Works as well: string s = HttpContext.Current.Request.UserHostAddress;

ipaddress =
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];


if (ipaddress == “” || ipaddress == null)


ipaddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];


return ipaddress;
}
For continued reading, check these out:
X-Forwarded-For
Useful IP Testing Platform


Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>