Azure Function 2.x - 获取当前用户的声明 [英] Azure Function 2.x - Get Current User's Claims
问题描述
我有一个 Azure Function 2.x (Asp.net Core) 并且正在使用 Azure AD 进行身份验证.我正在尝试在身份验证后访问登录用户的声明.以前使用 Azure Functions 1.x,我们将使用 ClaimsPrincipal.Current 获取声明,如下面的代码所示:
I have an Azure Function 2.x (Asp.net Core) and am authenticating with Azure AD. I'm trying to access the logged-in user's Claims after authentication. Previously using Azure Functions 1.x we would get the Claims using ClaimsPrincipal.Current, as seen in the code below:
using System.Net;
using System.Collections.Generic;
using System.Security.Claims;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
public static HttpResponseMessage Run(HttpRequestMessage req, out object document, TraceWriter log)
{
string name = ClaimsPrincipal.Current.FindFirst("name").Value;
log.Info($"name is {name}");
return req.CreateResponse(HttpStatusCode.OK, "Done");
}
关于我们如何使用 .Net Core 在 Azure Functions 2.x 中访问声明的任何指导?
Any guidance on how we access Claims in Azure Functions 2.x using .Net Core?
推荐答案
这个功能是 现在在 Azure Functions 2.0 的 C# 中支持.您现在可以将 ClaimsPrincipal
作为参数添加到您的 HttpTrigger
函数的签名中,或者您可以通过 req 在
.HttpRequest
对象上访问它.HttpContext.User
This feature is now supported in C# in Azure Functions 2.0. You can now add ClaimsPrincipal
as a parameter to your HttpTrigger
function's signature, or you can access it on the HttpRequest
object via req.HttpContext.User
.
应该很快就会支持 JavaScript,最终所有语言都应该支持这个功能.
Support should be coming soon to JavaScript, and eventually all languages should support this feature.
这篇关于Azure Function 2.x - 获取当前用户的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!