3

I can't find a question or post specific to Azure for this, and I'm not sure what's different in the environment in Azure versus my testing environments that would cause this. I've tried a few methods to get this to work but I'm not coming right. Please note this isn't Webapi, so using the HttpRequestMessage, as far as I know, is not going to work either.

Here's what I've tried so far:

Method 1:

string ipAddress = "";
IPHostEntry Host = default(IPHostEntry);
Host = Dns.GetHostEntry(System.Environment.MachineName);
ipAddress = Host.AddressList.SingleOrDefault(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).MapToIPv4().ToString();

Method 2:

string userIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(userIP))
{
    userIP = Request.ServerVariables["REMOTE_ADDR"];
}
return userIP;

Thanks in advance.

Topher
  • 1,011
  • 11
  • 19

1 Answers1

3

Read the value transported in this header:

x-forwarded-for: "91.23.44.24:52623"

Note there's a source port number trailing the IP address, so parse accordingly.

Also, as @NicoD correctly points out, the header in question may contain an array of proxy servers the request traversed. For example:

x-forwarded-for: "91.23.44.24:52623, 91.23.44.155"

Syntax

X-Forwarded-For: <client>, <proxy1>, <proxy2>

<client> The client IP address

<proxy1>, <proxy2> If a request goes through multiple proxies, the IP addresses of each successive proxy is listed. This means, the right-most IP address is the IP address of the most recent proxy and the left-most IP address is the IP address of the originating client.

What about that glaring port number you ask?

From https://www.rfc-editor.org/rfc/rfc7239:

5.2. Forwarded For

[...] this parameter MAY instead contain an IP address (and, potentially, a port number).

All client requests are terminated in the frontend layer and proxied via Application Request Routing to your web worker (hosting the MVC5 application).

App Service Application Request Routing

Community
  • 1
  • 1
evilSnobu
  • 24,582
  • 8
  • 41
  • 71
  • Thanks. Is there a way to do it differently from how I tried above in my example of things I tried? Did I do something incorrectly there? – Topher Feb 05 '18 at 09:09
  • There is no client IP information available to your web worker's environment, so the traditional `Request.ServerVariables` won't work in App Service. You did nothing wrong, it just doesn't apply here. – evilSnobu Feb 05 '18 at 09:29
  • There's also this way - https://stackoverflow.com/a/46516146/4148708 - but i'm not sure how that translates to Full Framework ASP.NET. Checking the header yourself should be faster since you don't instantiate anything extra. – evilSnobu Feb 05 '18 at 09:31
  • Thanks, I've accepted this because as you said, the port number is included and needs to be removed for the rest of the code to work - which wasn't taken into account when the guys tested using the header method. I don't think they saw the output unfortunately. – Topher Feb 05 '18 at 10:08
  • 2
    Note that x-forwarded-for can contain multiple IP addresses if the request is chained by proxy servers : X-Forwarded-For: , , . see [X-Forwarded-For](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For) – NicoD Feb 05 '18 at 17:08
  • Very good point, adding that in. There's apparently an RFC that's cool with the port number as well. – evilSnobu Feb 05 '18 at 17:27