如何在dotnet隔离的Azure函数中获取客户端IP地址?

发布于 2025-02-10 16:29:51 字数 540 浏览 1 评论 0原文

我正在使用Azure函数隔离在.NETCORE 6.0(C#)中编写功能,并且需要获取客户端的IP地址。 有什么方法可以从httprequestdata或functionContext对象获取客户端IP地址?

   [Function("GetClientIP")]
public async Task<HttpResponseData> GetClientIP([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req, FunctionContext functionContext)
{  ....  }

我已经提到了 link :但这不是用于隔离模式。

备注:我正在使用隔离模式。

I'm writing a function in .NetCore 6.0 (C#) using Azure Functions Isolation and need to get the ip address of the client.
Is there any way to get Client IP address from the HttpRequestData OR FunctionContext object?

   [Function("GetClientIP")]
public async Task<HttpResponseData> GetClientIP([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req, FunctionContext functionContext)
{  ....  }

I have referred following link: but it is not for ISOLATION mode.

Remarks: I am using ISOLATION mode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦魇绽荼蘼 2025-02-17 16:29:51

我终于可以通过下面的代码来检索它。

请注意,仅在Azure内托管时,标题值“ X-Forwarded-For”才能可用。

public async Task<HttpResponseData> SendMessage(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req,
    FunctionContext executionContext, 
    string requestName)
{
    var headerDictionary = req.Headers.ToDictionary(x => x.Key, x => x.Value, StringComparer.Ordinal);
    var key = "x-forwarded-for";
    if (headerDictionary.ContainsKey(key))
    {
        IPAddress? ipAddress = null;
        var headerValues = headerDictionary[key];
        var ipn = headerValues?.FirstOrDefault()?.Split(new char[] { ',' }).FirstOrDefault()?.Split(new char[] { ':' }).FirstOrDefault();
        if (IPAddress.TryParse(ipn, out ipAddress))
        {
            var ipAddressString = ipAddress.ToString();
        }
    }
}

在我的情况下,检索的值包含以下值
“ 105.224.244.204,147.243.88.136:58088”
列表中的第一个IP地址包含客户端IP地址。

我还发现我本可以用键值“ X-azure-clientip”检索它。原因是该功能托管在Azure前门后面。

该链接更详细地介绍了Azure前门后门托管时可以预期的标题
https://learlen.microsoft.com/en -us/azure/前门/前门 - http-headers-protocol

I was finally able to retrieve it by using the code below.

Please note that the header value "x-forwarded-for" is only available when hosted within azure.

public async Task<HttpResponseData> SendMessage(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req,
    FunctionContext executionContext, 
    string requestName)
{
    var headerDictionary = req.Headers.ToDictionary(x => x.Key, x => x.Value, StringComparer.Ordinal);
    var key = "x-forwarded-for";
    if (headerDictionary.ContainsKey(key))
    {
        IPAddress? ipAddress = null;
        var headerValues = headerDictionary[key];
        var ipn = headerValues?.FirstOrDefault()?.Split(new char[] { ',' }).FirstOrDefault()?.Split(new char[] { ':' }).FirstOrDefault();
        if (IPAddress.TryParse(ipn, out ipAddress))
        {
            var ipAddressString = ipAddress.ToString();
        }
    }
}

In my case the retrieved value contained the following value
"105.224.244.204, 147.243.88.136:58088"
The first IP address in the list contains the client IP address.

I also discovered that I could have retrieved it with key value "x-azure-clientip". The reason for this is the function is hosted behind Azure Front Door.

The link goes into more detail about what headers can be expected on the request when hosted behind Azure Front Door
https://learn.microsoft.com/en-us/azure/frontdoor/front-door-http-headers-protocol

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文