I've tried various ways to get the IP address such as mine when visiting a .aspx page. I'm connected through a AT&T cellular hotspot device and I get an IP address which is incorrect. If I google "whats my IP address" google gets it right. I know this because I was trying to get access to something through my firewall which I needed my IP address for and it wasn't working. I verified Google's was in fact correct. But why are they different? I've browsed various resources on the web and some get what I return in .NET and some get what Google is showing. Here is the code I'm using which is not returning the correct IP address.
protected string GetIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
I have also tried simply httpContext.Current.UserHostAddress which also returns the incorrect IP address.