Checking the request.Headers["X-Forwarded-For"] parameter should work when the function app is published on Azure (not locally).
Getting Client-IP from Azure Functions - .NET Isolated
I have seen many solutions of getting client IP address which are from Azure Functions .Net 3.1, 6, JavaScript, etc.
Our Project belongs to .NET Core 6 Isolated Version. So, can you help me how to get the Client-IP using HttpRequestData
class present in HttpTrigger .Net 6 Isolated
Azure Function?
I have tried the following code snippets to get, but unfortunately failed:
-
var remoteAddress = req.HttpContext.Connection.RemoteIpAddress;
To try this line, I'm not getting the HttpContext object of request.
- Few Code snippets present in this SO Reference are not working in .NET 6 Isolated.
- Found another SO Reference which given me the code snippet of .NET 5 Isolated, but that doesn't worked for me in the same version of .NET 5 isolated also.
Azure Functions
2 answers
Sort by: Most helpful
-
-
MughundhanRaveendran-MSFT 12,506 Reputation points
2022-07-04T07:21:41.177+00:00 @NFSCoder-9821 ,
If you are willing to integrate an APIM in front of Azure function, then the Client IP can be passed along to the Function App by way of defining a header via a small policy snippet:
<set-header name="X-Forwarded-Client-Ip" exists-action="override">
<value>@(context.Request.IpAddress)</value>
</set-header>APIM maintains Client IP through context variable as follows: context.Request.IpAddress
The corresponding value then can be accessed in your HttpTrigger function via HttpRequest object as follows:
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
....var clientIP = req.Headers["X-Forwarded-Client-Ip"]; ....
}
This way you can get the client ip of the function app