Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request.UserHostAddress return IP address of Load Balancer

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

Anyway, what I wanted to do was capture the IP address of the user who came to the site. I therefore, used Request.UserHostAddress to do that. On development, as I said, it gave me the loopback address, which is correct, since I was running the site from my machine. On live it did something entirely different. It always returned the address of the load balancer.

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

Thanks,

Sachin

like image 804
Sachin Kainth Avatar asked Mar 08 '13 15:03

Sachin Kainth


People also ask

How do I find the IP address behind a load balancer?

You can determine the IP addresses associated with an internal load balancer or an internet-facing load balancer by resolving the DNS name of the load balancer. These are the IP addresses where the clients should send the requests that are destined for the load balancer.

What is request ServerVariables Http_x_forwarded_for?

ServerVariables HTTP_X_FORWARDED_FOR is NULL when it is used to fetch the IP Address in ASP.Net using C# and VB.Net. Download Code Sample View Demo. Download Free Word/PDF/Excel API. In this article I will explain with an example, why the Request.

Does ELB have IP address?

For more information, see Routing traffic to an ELB load balancer in the Amazon Route 53 Developer Guide. The load balancer has one IP address per enabled Availability Zone. These are the addresses of the load balancer nodes.


2 Answers

From within your own application, if nothing else has been done to help you, you're stuck. That's as much information as is available to you.

If you're lucky, your load-balancer has been configured to add one or more extra 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 a de facto standard for identifying the originating IP address of a client connecting to a web server through an HTTP proxy or load balancer.

which you would then access via the Request.Headers property.

But discovering whether this (or another) header is available is not something we can help with - you need to talk to the people who configured the load balancer for your organization.

like image 175
Damien_The_Unbeliever Avatar answered Oct 10 '22 16:10

Damien_The_Unbeliever


In reference to @Damien_The_Unbeliever's answer, here's 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;
}
like image 36
Shimmy Weitzhandler Avatar answered Oct 10 '22 14:10

Shimmy Weitzhandler