是否可以向AzureAdBearer令牌添加多个受众?

问题描述 投票:0回答:1

我有一个使用MSAL从Azure AD获取访问令牌的应用程序。这是在客户端完成的,它拥有自己的Azure App Registry。然后,我将此访问令牌传递给服务器,然后服务器调用Microsoft Graph以代表用户获取其他信息。但是,当我进行图形调用时,我得到的观众与失败不匹配。这似乎是合适的,但现在我不确定如何将Web服务器受众ID添加到令牌。

首先,这是这种情况的正确工作流程吗?我正在使用MSAL对用户进行身份验证,然后使用Web服务调用图表以获取更多信息。其次,如果使用AzureADBearer,是否可以向访问令牌添加多个受众?我知道JWTBearer可以做到这一点。

//web service
services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;

        });

        services.AddAuthentication(sharedOptions =>
        {
            sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;

        })
        .AddAzureAdBearer(options =>
        {
            Configuration.Bind("AzureAd", options);

        })
        .AddCookie(options => options.Cookie.SameSite = SameSiteMode.None);
asp.net-core azure-active-directory msal
1个回答
2
投票

概念

您描述流程的方式看起来就像您正在使用来自客户端到webservice的令牌直接代表用户调用Microsoft Graph API ..这代表流不正确。

在基于传入令牌在Web服务中验证客户端之后,Web服务应该代表用户专门为Microsoft图表获取新令牌(使用第一个令牌)。

在这里查看更详细的流程Azure Active Directory v2.0 and OAuth 2.0 On-Behalf-Of flow

代码示例

看一下这段代码示例。它与你想要达到的目标非常接近。

这里的WPF应用程序首先调用ASP.NET Core Web API,然后API代表用户调用Microsoft Graph。

ASP.NET Core 2.1 Web API calling Microsoft Graph, itself called from a WPF application using Azure AD V2

从代码中注意到的重要部分:

查看TodoListController.cs,其中API首先代表用户获取新的AccessToken,传入所需的范围,然后使用此新令牌调用Microsoft Graph API。

 public async Task<string> CallGraphApiOnBehalfOfUser()
 {
    string[] scopes = { "user.read" };

    // we use MSAL.NET to get a token to call the API On Behalf Of the current user
    try
    {
        string accessToken = await _tokenAcquisition.GetAccessTokenOnBehalfOfUser(HttpContext, scopes);
        dynamic me = await CallGraphApiOnBehalfOfUser(accessToken);
        return me.userPrincipalName;
    }
© www.soinside.com 2019 - 2024. All rights reserved.