问题
I need to create a simple Azure Function App that gives me the username who access it.
I have enabled Azure Active Directory "Authentication/Authorization" and when I access the function URL it prompts for the user and I can login well
In Function App log I want to see the user who have logged. How can I do it?
回答1:
App Service passes user claims to your application by using special headers. External requests aren't allowed to set these headers, so they are present only if set by App Service.
You could use X-MS-CLIENT-PRINCIPAL-NAME as http resquest header to get the username.
var name1=httpRequest.Headers["X-MS-CLIENT-PRINCIPAL-NAME"].ToString();
Also, 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)
{
var name1=httpRequest.Headers["X-MS-CLIENT-PRINCIPAL-NAME"].ToString();
var name2 = claimsPrincipal.Identity.Name;
}
Note:
When you add App registrations in Azure ad, add redirect url as https://yourfunctionname.azurewebsites.net/.auth/login/aad/callback
and click ID token
when you setting Advanced settings
.
来源:https://stackoverflow.com/questions/58125338/azure-function-app-gives-me-the-user-logged