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: