Getting Client-IP from Azure Functions - .NET Isolated

krishna572 886 Reputation points
2022-06-24T14:12:30.657+00:00

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:

  1. var remoteAddress = req.HttpContext.Connection.RemoteIpAddress;

To try this line, I'm not getting the HttpContext object of request.

  1. Few Code snippets present in this SO Reference are not working in .NET 6 Isolated.
  2. 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
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Stefano Demiliani 1 Reputation point
    2022-06-24T15:10:07.123+00:00

    Checking the request.Headers["X-Forwarded-For"] parameter should work when the function app is published on Azure (not locally).


  2. 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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.