问题描述
我创建了一个新的Function App,为其启用了App Service身份验证/授权("使用身份验证/授权来保护您的应用程序并使用每个用户的数据"),并禁用了未经身份验证的功能请求.
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.
到目前为止,一切似乎都可以正常工作.如果我尝试请求我的HttpTrigger
ed函数,它需要我先登录;否则,我必须先登录.登录后,所有请求都将按应有的方式进行处理.因此,保护您的应用程序"部分没有问题.
Everything seems to be working correctly so far. If I try to request my HttpTrigger
ed 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运行时v2.0.12309 ,您可以检索来自 ClaimsPrincipal 实例通过Run
方法注入:
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中获取当前用户身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!