无法从客户端凭据身份验证Microsoft AD授权令牌

时间:2020-09-03 03:30:46

标签: c# asp.net-core active-directory azure-active-directory

因此,我已经使用ASP Net Core构建了一个应用程序。 这是我的代码

    services.AddAuthentication(AzureADDefaults.JwtBearerAuthenticationScheme)
    .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));

    services.Configure<JwtBearerOptions>(AzureADDefaults.JwtBearerAuthenticationScheme, options =>
    {
        // This is a Microsoft identity platform web API.
        options.Authority += "/v2.0";

        // The web API accepts as audiences both the Client ID (options.Audience) and api://{ClientID}.
        options.TokenValidationParameters.ValidAudiences = new[]
        {
         options.Audience,
         $"api://{options.Audience}"
        };
    });

    

当我使用用户密码验证时,此设置工作正常。但是由于某些条件,我只能对其他应用程序使用client_credentials。我正在使用它来获取令牌 enter image description here 我成功获得了令牌,但是当我使用令牌时,它会导致我未经授权

这是我使用的API权限 enter image description here

这是我的解码令牌 enter image description here

1 个答案:

答案 0 :(得分:2)

请将范围更改为:api://{ClientID}/.default.


更新

您需要创建另一个代表Web api的Azure AD应用程序,然后使用您的客户端应用程序调用 web api应用程序

首先,您需要公开代表Web api的应用程序的api,可以按照以下过程进行配置:

Azure门户 应用程序注册> 公开API > 添加范围> 添加客户应用程序

接下来,您需要定义api应用程序的清单并向您的客户端应用程序授予应用程序权限(这是您自己定义的角色权限,添加权限后,您可以在我的API 中找到它)

This是定义清单的过程。

enter image description here

这是授予客户端应用程序权限: enter image description here

最后,您可以为您的api应用程序请求令牌: enter image description here