HOW TO RETURN CLIENT IP IN AZURE FUNCTIONS

Dhruv Soni 0 Reputation points
2024-07-29T10:18:46.35+00:00

Hi Reader !

I need to get client IP in azure functions.

Need to create such function which could get me Client IP i.e the IP of the machine which calls the function. And i want to call the function using business central. But i am in doubt if it could be achieved.

I already Tried running certain scripts in powershell in azure function but none seem to do proper justice.

Would appreciate your help.

Thank you and have a great day ahead :)

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,114 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2024-07-29T10:18:46.3866667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To get the client IP in Azure Functions, you can use the HttpRequest object's HttpContext.Connection.RemoteIpAddress property. Here's an example code snippet in C#:

    using Microsoft.AspNetCore.Http;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        var clientIp = req.HttpContext.Connection.RemoteIpAddress.ToString();
        log.LogInformation($"Client IP: {clientIp}");
        return new OkObjectResult(clientIp);
    }
    

    This code snippet uses the HttpTrigger attribute to create an HTTP-triggered Azure Function that accepts GET and POST requests. The HttpRequest object is used to get the client IP address, which is then logged and returned as the response.

    References:

    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.