如何使用 Azure 身份验证在 Azure Function 中获取当前用户身份?

时间:2023-02-27
本文介绍了如何使用 Azure 身份验证在 Azure Function 中获取当前用户身份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我创建了一个新的函数应用,为它启用了应用服务身份验证/授权(使用身份验证/授权来保护您的应用程序并使用每个用户的数据")并禁用非身份验证请求.

I have created a new Function App, enabled App Service Authentication / Authorization for it ("Use Authentication / Authorization to protect your application and work with per-user data") and disabled non-authenticated requests.

到目前为止,一切似乎都运行正常.如果我尝试请求我的 HttpTriggered 函数,它需要我先登录;登录后,所有请求都会按原样处理.所以保护您的应用程序"部分没有问题.

Everything seems to be working correctly so far. If I try to request my HttpTriggered function, it requires me to log in first; once I'm logged in, all requests are processed as they should be. So there is no problem with "protect your application" part.

但是,我完全被处理每个用户的数据"部分所困扰.我的 Azure 函数被调用为

However, I'm totally stuck with the "work with per-user data" part. My Azure Function is invoked as

public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)

HttpRequestMessage 中没有与身份验证相关的内容.(AuthorizationLevel.Anonymous 似乎控制着完全不同的事情——即,函数是否可以由任何人调用,或者只能由具有固定 API 密钥的人调用).

And there is nothing related to authentication in HttpRequestMessage. (AuthorizationLevel.Anonymous seems to control the entirely different thing - namely, if the function could be called by anyone or only by those who have a fixed API key).

如何获取调用该函数的经过身份验证的用户的身份?

How do I get the identity of authenticated user who called the function?

推荐答案

使用 Azure Function runtime v2.0.12309,你可以检索 来自 Run 方法中注入的 "noreferrer">ClaimsPrincipal 实例:

Using the Azure Function runtime v2.0.12309, you can retrieve the authenticated user information from the ClaimsPrincipal instance injected in the Run method:

public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
    HttpRequest httpRequest, 
    ILogger logger, 
    ClaimsPrincipal claimsPrincipal)
 {
            // Explores the authenticated user's claims in claimsPrincipal.
 }

这篇关于如何使用 Azure 身份验证在 Azure Function 中获取当前用户身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:无法从桌面控制台应用访问 Azure Key Vault 下一篇:使用密码和 Azure Active Directory 身份验证的 Asp.net Identity

相关文章

最新文章