我创建了一个示例Blazor WebAssembly应用程序,使用Microsoft文档文章中概述的步骤通过Azure B2C对其进行保护:https ://docs.microsoft.com/zh-cn/aspnet/core/blazor/security/webassembly/hosted-with -azure-active-directory-b2c?view = aspnetcore-3.1。
除身份验证外,该应用程序可以正常工作。B2C模态加载会登录,但是UI会显示以下错误消息。
There was an error trying to log you in: 'AADB2C90205: This application does not have sufficient permissions against this web resource to perform the operation.
这是我的B2C配置的一些经过编辑的屏幕截图(我确定该代码正确,因为它是从dotnet new
CLI命令生成的默认代码)。
对配置错误有任何想法吗?
因此,事实证明问题出在代码中。
我错过了文档中的注释部分,其中说如果API ID URI是不受信任的发布者域(类似于https://contoso.onmicrosoft.com/41451fa7-82d9-4673-8fa5-69eff5a761fd),则必须手动编辑客户端应用程序代码,该部分就我而言。
删除多余https://{TENANT DOMAIN}/
的Program.cs
的客户端应用程序固定的东西(注意注释掉的行,之后而来的线之间的差值)。
namespace BlazorClient
{
public class Program
{
public static async Task Main(string[] args)
{
...
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAdB2C", options.ProviderOptions.Authentication);
//options.ProviderOptions.DefaultAccessTokenScopes.Add("https://***.onmicrosoft.com/https://***.onmicrosoft.com/*******/blazor.client");
options.ProviderOptions.DefaultAccessTokenScopes.Add("https://***.onmicrosoft.com/*******/blazor.client");
});
await builder.Build().RunAsync();
}
}
}