How to get client IP address for isolated .net 8 running in Azure Functions (breaking change?)

325 Views Asked by At

We have been successfully using http header X-Forwarded-For in a .net 7 isolated worker process for Azure Functions to retrieve the originator's Client IP address.

We just upgraded to .net 8 and this header has disappeared. There appears to be nothing similar when I dump out all the passed headers.

I think this could be a regression/bug with running .net 8 in an isolated worker process for Azure Functions but perhaps someone knows of a reason/config change that needs to be made to make this work again?

I should say, in case it's relevant, that we're using Azure Functions as the API for Static Web App (via Bring Your Own Functions)

Good examples of how to extract this header can be found here - I would stress - this is not that our code is broken, it's that the header that used to exist no longer does (under some circumstances that I cannot pin down directly)

A bit more info on this... dumping out the headers I can see that when it works I get a comma-separated list of IP addresses for x-forwarded-for with my client IP address as the first one.

Also I see that the Host is the DNS entry to the Azure Functions.

Working headers Working headers

But, when it doesn't work, I get just the single Azure Data Centre IP (which is then used by ASP.NET Core integration middleware to set the RemoteIpAddress) and the Host is localhost:port

Not working headers Not working headers

Also note the capitalisation changes in the Header names - whilst I realise that doesn't matter as they are case insensitive, it does suggest that there are two different code-paths or configurations that are being used.

1

There are 1 best solutions below

1
Donny Kwitty On

Something like this:

public async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    ILogger log)
{
  var remoteIP = req.HttpContext.Connection.RemoteIpAddress;
}