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

How to get current user identity in Azure Function with Azure Authentication?(如何使用 Azure 身份验证在 Azure Function 中获取当前用户身份?)
本文介绍了如何使用 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 中获取当前用户身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to MOQ an Indexed property(如何最小起订量索引属性)
Mocking generic methods in Moq without specifying T(在 Moq 中模拟泛型方法而不指定 T)
How Moles Isolation framework is implemented?(Moles Isolation 框架是如何实现的?)
Difference between Dependency Injection and Mocking Framework (Ninject vs RhinoMocks or Moq)(依赖注入和模拟框架之间的区别(Ninject vs RhinoMocks 或 Moq))
How to mock Controller.User using moq(如何使用 moq 模拟 Controller.User)
How do I mock a class without an interface?(如何模拟没有接口的类?)