Blazor WASM 授权不适用于 AAD 角色

Tac*_*cot 5 asp.net azure-authentication blazor blazor-client-side blazor-webassembly

我正在尝试根据此文档https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/azure-active-directory-groups-and-根据用户定义的角色设置 AAD 授权roles?view=aspnetcore-3.1#user-defined-roles我可以在应用程序清单中设置它并使 API 授权工作。但是,当我尝试在 UI 端执行此操作时,我无法显示该声明。我做了 json 解释类(DirectoryObjects、CustomUserAccount 和 Value(由目录对象使用))。我还添加了删除组内容的 CustomUserFactory,因为我只关心角色:

        private readonly ILogger<CustomUserFactory> _logger;
        private readonly IHttpClientFactory _clientFactory;

        public CustomUserFactory(IAccessTokenProviderAccessor accessor,
            IHttpClientFactory clientFactory,
            ILogger<CustomUserFactory> logger)
            : base(accessor)
        {
            _clientFactory = clientFactory;
            _logger = logger;
        }

        public async override ValueTask<ClaimsPrincipal> CreateUserAsync(
            CustomUserAccount account,
            RemoteAuthenticationUserOptions options)
        {
            var initialUser = await base.CreateUserAsync(account, options);

            if (initialUser.Identity.IsAuthenticated)
            {
                var userIdentity = (ClaimsIdentity)initialUser.Identity;

                foreach (var role in account.Roles)
                {
                    userIdentity.AddClaim(new Claim("role", role));
                }

                
            }

            return initialUser;
        }
Run Code Online (Sandbox Code Playgroud)

然后我按照文档中提到的那样修改了 program.cs:

    builder.Services.AddMsalAuthentication<RemoteAuthenticationState,
    CustomUserAccount>(options =>
    {
        builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
        options.ProviderOptions.DefaultAccessTokenScopes.Add("apiaccessguid");
        options.UserOptions.RoleClaim = "role";
    }).AddAccountClaimsPrincipalFactory<RemoteAuthenticationState, CustomUserAccount,
    CustomUserFactory>();
Run Code Online (Sandbox Code Playgroud)

当那不起作用时,我尝试将其添加为一项政策,但也没有运气:

 builder.Services.AddAuthorizationCore(options =>
    {
        options.AddPolicy("Admin", policy =>
            policy.RequireClaim("role", "admin"));
    });
Run Code Online (Sandbox Code Playgroud)

用于限制我在代码中尝试的视图user.IsInRole("admin")和在 UI 中使用

<AuthorizeView Roles="admin">
    <li class="nav-item px-3">
        <NavLink class="nav-link" href="Admin">
            Admin
        </NavLink>
    </li>
</AuthorizeView>
Run Code Online (Sandbox Code Playgroud)

并有政策:

<AuthorizeView Policy="Admin">
    <Authorized>
        <p>
            The user is in the 'Administrator' AAD Administrative Role
            and can see this content.
        </p>
    </Authorized>
    <NotAuthorized>
        <p>
            The user is NOT in the 'Administrator' role and sees this
            content.
        </p>
    </NotAuthorized>
</AuthorizeView>
Run Code Online (Sandbox Code Playgroud)

他们都没有工作。有什么我想念的吗?我还验证了令牌具有管理员角色。

Tac*_*cot 1

发现我的问题,代码没问题,问题出在Azure注册端,客户端使用Azure中客户端应用程序中注册的角色,而服务器使用服务器应用程序中的角色。因此,请确保您在两者中注册具有相同角色的用户。