【问题标题】:Blazor WASM Authorization not working with AAD RolesBlazor WASM 授权不适用于 AAD 角色
【发布时间】:2020-12-09 02:33:57
【问题描述】:

我正在尝试根据此文档https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/azure-active-directory-groups-and-roles?view=aspnetcore-3.1#user-defined-roles 之后的用户定义角色设置 AAD 授权,我可以在应用清单中设置它并让 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;
        }

然后我按照文档中提到的那样修改了 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>();

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

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

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

<AuthorizeView Roles="admin">
    <li class="nav-item px-3">
        <NavLink class="nav-link" href="Admin">
            Admin
        </NavLink>
    </li>
</AuthorizeView>

和政策:

<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>

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

【问题讨论】:

  • 你不是少了一个“s”到“role”吗? documentation 声明经过身份验证的用户将在 roles 声明中收到分配给他们的角色。
  • 我试过了,但没有成功,我认为角色是正确的,因为在 CustomUserFactory 的末尾我做了一个角色的 forloop,并添加了“角色”的声明,这就是它说在这个文档中做docs.microsoft.com/en-us/aspnet/core/blazor/security/…

标签: asp.net blazor blazor-client-side azure-authentication blazor-webassembly


【解决方案1】:

我通过在策略选项中使用 RequireRole 使其正常工作。

例如,我将应用角色添加到清单中:

"appRoles": [
    {
        "allowedMemberTypes": [
            "User"
        ],
        "description": "Reader role.",
        "displayName": "Reader",
        "id": "41d9ba42-456e-4471-8946-24216e5f6c64",
        "isEnabled": true,
        "lang": null,
        "origin": "Application",
        "value": "Reader"
    }
]

通过 RequireRole 配置策略:

builder.Services.AddAuthorizationCore(options =>
{
    options.AddPolicy("app-reader", policy => policy.RequireRole("Reader"));
});

然后按以下方式使用:

<AuthorizeView Policy="app-reader">
    <Authorized>
        <p>
            The user is in the 'Reader' Role
            and can see this content.
        </p>
    </Authorized>
    <NotAuthorized>
        <p>
            The user is NOT in the 'Reader' role 
            and sees this content.
        </p>
    </NotAuthorized>
</AuthorizeView>

或者,作为剃须刀页面上的属性:

@attribute [Authorize(Policy = "app-reader")]

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 2020-10-29
      • 2021-02-24
      • 2022-12-16
      • 1970-01-01
      • 2011-05-17
      • 2015-09-06
      • 2020-03-13
      • 2020-08-27
      相关资源
      最近更新 更多