Blazor Wasm 托管。 .Net 6.0。问题 AAD 身份验证

Blazor Wasm Hosted. .Net 6.0. Problem AAD Authentication

当我尝试在 Blazor WebAssembly 托管和 .Net 6.0 中使用 AAD 进行身份验证时,在身份验证之后,我在尝试访问 /WheaterForecast 控制器时遇到以下错误:

WWW-Authenticate: Bearer error="invalid_token", error_description="受众'api://11111111-1111-1111-1111-111111111111'无效"

在 AAD 中,我有以下内容:
服务器应用程序
客户编号:11111111-1111-1111-1111-111111111111
租户编号:22222222-2222-2222-2222-222222222222
URI ID 应用程序:api://11111111-1111-1111-1111-111111111111

客户端应用程序
客户编号:33333333-3333-3333-3333-333333333333

然后,我的 Server 应用程序配置是:

"AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "xxxxxx.emea.microsoftonline.com",
    "TenantId": "22222222-2222-2222-2222-222222222222",
    "ClientId": "api://11111111-1111-1111-1111-111111111111",
    "CallbackPath": "/signin-oidc"
  }

在 program.cs ...

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
...
...
app.UseAuthentication();
app.UseAuthorization();

我的客户端应用程序配置...

"AzureAd": {
"Authority": "https://login.microsoftonline.com/fff3a238-b26a-4351-8a2a-0b19dc72e02e",
"ClientId": "33333333-3333-3333-3333-333333333333",
"ValidateAuthority": true

}

Program.cs 来自客户端应用...

builder.Services.AddHttpClient("ReservasSalasAuth.ServerAPI", client =>
 client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress))
.AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();

builder.Services.AddScoped(sp => sp.GetRequiredService<IHttpClientFactory>().CreateClient("{MyNamespace}.ServerAPI"));
 
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
options.ProviderOptions.DefaultAccessTokenScopes.Add("api://11111111-1111-1111-1111-111111111111/API.Read");
});

最后一点不好闻的是我几乎总是登录,弹出窗口一直停留直到我在 Edge 中按 F12...然后继续并完成弹出窗口。

提前致谢!!

受众值是客户端 ID 或应用程序 ID URI,根据应用程序在应用程序注册中设置为“公开 api”blade。

请检查可能的解决方法:

  1. 检查 http://jwt.io 中的 jwt 令牌。如果发行人即; “iss”:“https://login.microsoftonline.com/XXXXXX/v2.0”,有 v2 enpoint.Check 你的 azure 广告应用程序的清单:下面的值可能是 null 或 1,应该是 2 :"accessTokenAcceptedVersion": 2, 检查当前应用程序注册是否相同,如果需要修改并尝试登录。

(或)

  1. 首先尝试通过删除 api:// 服务器 api 设置中客户端 ID 中的 api:// 登录。如果没有,也删除 api:// 中 options.ProviderOptions.DefaultAccessTokenScopes.Add 的前缀客户端配置服务。

(或)

  1. 在客户端应用程序中,对于隐式授权,select 访问令牌和 ID 令牌的复选框。 确保在门户中添加 api permissions 与代码配置相同,并将其添加到范围并授予权限)。 确保您使用工作或学校帐户登录。

请检查Secure an ASP.NET Core Blazor WebAssembly hosted app with Azure Active Directory | Microsoft Docsaccess token scopes下的内容,检查是否double api:// 方案存在并尝试添加 ap:// 或根据需要从范围中删除。