authentication - 带有角色的 Blazor WebAssembly AuthorizedView
问题描述
如果用户处于 GRP_FAST_ADMIN 角色,我有一个 Blazor 页面应该显示一个按钮。出于测试目的,如果他们不在角色中,我会打印出他们所在角色的列表。问题是他们的声明确实表明他们拥有正确的角色,但他们仍然没有被授权。
@page "/RestartApplication"
<AuthorizeView Roles="GRP_FAST_ADMIN">
<Authorized>
<button class="btn-default" @onclick="() => ViewModel.RestartApplication()">Restart Application</button>
</Authorized>
<NotAuthorized>
@if(Claims !=null)
{
@foreach(var claim in Claims)
{
<p>@(claim.Type.ToString() + ": " + claim.Value.ToString())</p>
}
}
</NotAuthorized>
</AuthorizeView>
该部分的输出:
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: CM_GENERAL_USER
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: ES_GENERAL_USER
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: GRP-PEOPLESOFT-P-GL_GENERAL
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: GRP_AWB_ADMIN
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: GRP_FAST_ADMIN
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: GRP_ILG_RO
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: IB_PWR_USER
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: IBP_POWER_USER
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: MP_ADMIN_GRP
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: OFB_GENERAL_USER
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: PK_GENERAL_USER
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: PT_GENERAL_USER
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: PT_PWR_USER
http://schemas.microsoft.com/ws/2008/06/identity/claims/role: RESORT_OPS_UTILITY
基本身份验证有效。我可以成功地用于确保某人已登录。在 WebAssembly 的启动中设置授权:
public static IServiceCollection AddPowerToolsWebServices(this IServiceCollection services)
{
services.AddDevExpressBlazor();
services.AddBlazoredLocalStorage();
services.AddAuthorizationCore();
services.AddScoped<TokenAuthenticationStateProvider>();
services.AddScoped<AuthenticationStateProvider, TokenAuthenticationStateProvider>(provider =>
provider.GetRequiredService<TokenAuthenticationStateProvider>());
return services;
}
我正在处理 GetAuthenticationStateAsync() 的覆盖
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var token = await GetTokenAsync();
if (string.IsNullOrWhiteSpace(token))
return _Anonymous;
_HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(ParseClaimsFromJwt(token), "jwt")));
}
这会解析所有声明并获取每个角色并将它们单独添加为声明。不知道我错过了什么。
解决方案
看起来您将不得不使用AccountClaimsPrincipalFactory
.
Blazor 期望的声明类型只是“角色”而不是“http://schemas.microsoft.com/ws/2008/06/identity/claims/role”。工厂可以为您实现这一点,然后您将能够使用标准的 blazor 组件/机制来管理它们。
即使使用个人用户帐户选项,您也必须这样做,因为声明以逗号分隔的字符串形式出现。
这是一个示例的链接。这不是您的情况,但解决方案与您需要将您拥有的声明转换为 blazor 期望的解决方案相同。
推荐阅读
- c# - 实体框架上下文 SaveChanges 抛出 NULL 错误
- python - 如何在 Python 中隐藏“输入()”?
- mysql - MySQL MDL 何时阻塞?什么时候可以超时等待它?
- docker - Docker:如何将容器加入主机网络?
- flutter - Flutter & AlertDialog : 如何拉伸容器使其与 AlertDialog 框具有相同的宽度?
- windows - Directory Table Base 分 4k,但我的 windows DTB / 4k = x ... 2
- javascript - 关闭选择选项后获取视图内下拉列表的值
- javascript - 如何在发出的事件调用函数之外调用函数
- elasticsearch - 根据可能不存在的日期字段对查询进行排序和查询
- javascript - 未定义的索引:_SESSIONS 中的 userId