Search code examples
azureazure-functions

How to get client IP address in Azure Functions C#?


I'm writing a function in C# using Azure Functions and need to get the ip address of the client that called the function, is this possible?


Solution

  • Here is an answer based on the one here.

    #r "System.Web"
    
    using System.Net;
    using System.Web;
    
    public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log)
    {
        string clientIP = ((HttpContextWrapper)req.Properties["MS_HttpContext"]).Request.UserHostAddress;
        return req.CreateResponse(HttpStatusCode.OK, $"The client IP is {clientIP}");
    }