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;
}
|
Tags: address, c#, ip, servervariables