Request.UserHostAddress returns load balancing IP address

I have a critical line of code on my site that worked in our development environment, but not in production. Well, I say that it worked in development, but the truth is that it gave ::1 , which is the IPv6 loop address.

In any case, what I wanted to do was capture the IP address of the user who came to the site. So I used Request.UserHostAddress for this. As it developed, as I said, he gave me a loopback address, which is correct, since I started the site from my machine. On the living it did something completely different. It always returned the load balancing address.

What I'm trying to understand is this. Was I wrong to use Request.UserHostAddress to grab the user's IP address or something was wrong with our network setup or something else?

Thanks,

Sechin

+5
source share
3 answers

From your own application, if nothing is done to help you, you are stuck. This is what is available to you.

If you're lucky, your load balancer is configured to add one or more additional headers with information about the original request.

One common solution is the X-Forwarded-For header:

The X-Forwarded-For (XFF) HTTP header field is the de facto standard for identifying the outgoing IP address of a client connecting to a web server through an HTTP proxy or load balancer.

access to which you could get through the Request.Headers property.

But to find out whether this (or another) header is available, we cannot help - you need to talk to people who have configured a load balancer for your organization.

+9
source

Regarding @Damien_The_Unbeliever answer , here is the complete solution:

 public static string GetIpAddress() { var request = HttpContext.Current.Request; // Look for a proxy address first var ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"]; // If there is no proxy, get the standard remote address if (string.IsNullOrWhiteSpace(ip) || string.Equals(ip, "unknown", StringComparison.OrdinalIgnoreCase)) ip = request.ServerVariables["REMOTE_ADDR"]; else { //extract first IP var index = ip.IndexOf(','); if (index > 0) ip = ip.Substring(0, index); //remove port index = ip.IndexOf(':'); if (index > 0) ip = ip.Substring(0, index); } return ip; } 
+3
source

Used this code to check production environment ... It worked for me:

  System.Web.HttpRequest oRequest = System.Web.HttpContext.Current.Request; string header; string ip; header = "HTTP_X_FORWARDED_FOR"; ip = oRequest.ServerVariables[header]; Response.Write(string.Format("{0} - {1}", header, ip) + Environment.NewLine); header = "REMOTE_ADDR"; ip = oRequest.ServerVariables[header]; Response.Write(string.Format("{0} - {1}", header, ip) + Environment.NewLine); header = "HTTP_CLIENT_IP"; ip = oRequest.ServerVariables[header]; Response.Write(string.Format("{0} - {1}", header, ip) + Environment.NewLine); header = "Request.UserHostAddress"; ip = oRequest.UserHostAddress; Response.Write(string.Format("{0} - {1}", header, ip) + Environment.NewLine); 
+2
source

All Articles