0

我在尝试从注册的 API 访问信息时收到此错误(Bearer error="invalid_token", error_description="The Audience 'xxxx-xxxx-xxxx-xxxx' is invalid"})。受众是 Azure ADB2C 中注册 API 的 clientId。我检查了访问令牌,它也具有与令牌的 aud 密钥相同的值。

我在托管 API 的应用程序中的启动配置如下:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)                
                .AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAd"));

调用 API 的 Web 应用在启动配置中具有以下设置

    services.AddAuthentication(AzureADDefaults.AuthenticationScheme);
                services.AddMicrosoftIdentityWebAppAuthentication(Configuration, "AzureAd")
                    .EnableTokenAcquisitionToCallDownstreamApi(ScopeConstants.SCOPES)
.AddDistributedTokenCaches();
4

1 回答 1

0

尽管它与基本的 AzureAd 参数设置没有直接关系,但我已经设法得到了解决方案。我的启动配置中有以下代码行。删除它们后,错误消失了。

services.AddControllersWithViews(options =>
            {
                var policy = new AuthorizationPolicyBuilder()         
                    .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
                    
                    .RequireScope(Configuration["AzureAd:Scopes"])
                    .Build();
                options.Filters.Add(new AuthorizeFilter(policy));
            });
于 2021-10-28T18:44:00.180 回答