Azure Function App-给我登录的用户 [英] Azure Function App - gives me the user logged

查看:42
本文介绍了Azure Function App-给我登录的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个简单的Azure Function应用程序,该应用程序为我提供访问它的用户名.

I need to create a simple Azure Function App that gives me the username who access it.

我已经启用了Azure Active Directory的身份验证/授权",当我访问功能URL时,它会提示用户,并且我可以很好地登录

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?

推荐答案

App Service通过使用特殊的标头将用户声明传递给您的应用程序.不允许外部请求设置这些标头,因此仅在由App Service设置时才存在.

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;
}

注意:

在Azure广告中添加应用程序注册时,添加

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.

这篇关于Azure Function App-给我登录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆