AzureADB2C GetAccessTokenForUserAsync错误“不支持请求中提供的作用域'user.read'”

时间:2020-09-07 08:58:59

标签: blazor azure-ad-b2c blazor-server-side msal microsoft-identity-platform

我正在尝试创建与Microsoft Graph API(特别是OneDrive)集成并使用Azure AD B2C进行身份验证的Blazor应用程序。

我正在使用Microsoft.Identity.Web 0.3.1-preview

Setup.cs如下:

        public void ConfigureServices(IServiceCollection services)
    {
        // Configuration to sign-in users with Azure AD B2C
        services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAdB2C")
             .EnableTokenAcquisitionToCallDownstreamApi(new string[] { "https://graph.microsoft.com/.default" })
             .AddMicrosoftGraph("https://graph.microsoft.com/beta")
             .AddInMemoryTokenCaches(); 
...

服务随后使用ITokenAcquisition 来请求令牌,如下所示:

public MyService(ITokenAcquisition tokenAcquisition, IOptions<WebOptions> webOptionValue, 
                          AuthenticationStateProvider AuthenticationStateProvider)
    {
                string result = await tokenAcquisition.GetAccessTokenForUserAsync(new string[] { "user.read" });

我正在使用Microsoft关联帐户进行身份验证,并且可以在AuthenticationStateProvider中看到live.com声明。

将基本user.read传递给GetAccessTokenForUserAsync会导致异常“不支持请求中提供的作用域'user.read'”。我尝试将范围指定为https://graph.microsoft.com/user.read,但这只会返回null。

任何建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

由Azure AD B2C发行的令牌供具有Azure AD B2C注册资源的Azure AD B2C注册客户端使用。

Microsoft Graph API不是Azure AD B2C注册的资源,因此不支持https://graph.microsoft.com/范围。

或者,您可以考虑将访问令牌作为the idp_access_token claim从Microsoft身份提供程序传递到Azure AD B2C注册的客户端。

the Microsoft identity providerpass this through需要自定义策略。