Azure Function App - gives me the user logged

一世执手 提交于 2020-08-10 06:12:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!