【发布时间】:2021-11-19 11:33:32
【问题描述】:
当我尝试在 Blazor WebAssembly Hosted 和 .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
那么,我的服务器应用配置是:
"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 ......然后继续并完成弹出窗口。
提前致谢!!
【问题讨论】:
标签: azure-active-directory blazor-webassembly .net-6.0