问题
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");
}
Any guidance on how we access Claims in Azure Functions 2.x using .Net Core?
回答1:
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
.
Support should be coming soon to JavaScript, and eventually all languages should support this feature.
来源:https://stackoverflow.com/questions/51730988/azure-function-2-x-get-current-users-claims