Get remote IP address in Azure function

2019-08-29 07:24发布

问题:

How can I get remote IP address in Azure function?

The MS_HttpContext property is not present in the properties of HttpRequestMessage, so I cannot use the solution here: How to get client IP address in Azure Functions C#?

Getting Forwarded For IP address is easy (from the headers, as shown in the link above), but how can I get the remote IP address?

回答1:

For .NET Core Functions, we usually use Microsoft.AspNetCore.Http.HttpRequest.

When we create a Http trigger template, we can see it.

    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)

Then get remote IP

var remoteAddress = req.HttpContext.Connection.RemoteIpAddress;