【问题标题】:.NET Core 3.1 - Bearer error="invalid_token", error_description="The audience 'xxxx-xxxx-xxxx-xxxx' is invalid"}.NET Core 3.1 - Bearer error="invalid_token", error_description="观众 'xxxx-xxxx-xxxx-xxxx' 无效"}
【发布时间】:2021-10-23 18:50:43
【问题描述】:

我在尝试从已注册的 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 的网络应用在启动配置中有如下设置

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

【问题讨论】:

  • 您是否关注this - 您需要将预期的 AppId 放入 appsettings.json 以进行 Web API 应用注册。在 Web 应用程序中,您必须放置一个在 Web API 应用程序注册上公开的范围值 - 显示为 here
  • @JasSuri-MSFT 提供的链接中的过程不起作用。我正在处理的业务案例确实是 B2C,但当前不支持或不包含在不记名令牌中的应用角色的限制意味着我不能使用用户流逻辑(即 SignUpSignInPolicyId)。标准 AzureAd 支持应用程序角色,但我被困在这个问题上。范围值在已共享的 Web 应用配置中表示为 ScopeConstants.SCOPES。即 public static List SCOPES = new List() { ReadWrite,Read };

标签: scope azure-ad-b2c target-audience


【解决方案1】:

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

services.AddControllersWithViews(options =>
            {
                var policy = new AuthorizationPolicyBuilder()         
                    .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
                    
                    .RequireScope(Configuration["AzureAd:Scopes"])
                    .Build();
                options.Filters.Add(new AuthorizeFilter(policy));
            });

【讨论】:

    猜你喜欢
    • 2021-02-06
    • 2020-01-06
    • 2016-09-08
    • 1970-01-01
    • 2018-08-03
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多