asp.net-core - Blazor WASM 中的授权 - 策略不起作用
问题描述
因此,当我将基于策略的授权添加到 Startup.cs 文件,然后将 AuthorizeView 添加到我的 Blazor 页面时,我收到一个错误:
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: The AuthorizationPolicy named: 'SuperAdmin' was not found.
System.InvalidOperationException: The AuthorizationPolicy named: 'SuperAdmin' was not found.
at Microsoft.AspNetCore.Authorization.AuthorizationPolicy.CombineAsync(IAuthorizationPolicyProvider policyProvider, IEnumerable`1 authorizeData)
at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.IsAuthorizedAsync(ClaimsPrincipal user)
at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.OnParametersSetAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle) blazor.webassembly.js:1:14889
但在我的启动中:
services.AddAuthorization(o =>
{
o.AddPolicy("SuperAdmin", policy => policy.RequireClaim("SuperAdmin"));
o.AddPolicy("CountyAdmin", policy => policy.RequireClaim("CountyAdmin"));
});
当我查看用户的“声明”时,没有显示来自数据库的声明。他们在那里。
那么,为什么,如果我在启动时声明了策略,那么标有上述策略的剃须刀页面是否会给我这个错误?
谢谢大家!!!
解决方案
我通过在 Main 函数中添加以下客户端的 Program.cs 来使其工作。
builder.services.AddAuthorizationCore(o =>
{
o.AddPolicy("SuperAdmin", policy => policy.RequireClaim("SuperAdmin"));
o.AddPolicy("CountyAdmin", policy => policy.RequireClaim("CountyAdmin"));
});
您在服务器上设置的授权。这是为客户做的。
推荐阅读
- php - 通过 PHP 授权 SOAP Web 服务
- python - 如何使用一个衬垫而不是“for loop”,它还包含 if-else 语句(给出的示例)
- php - PHP JSON utf-8:字符数不同
- c# - 在逻辑上表示单个值时将类转换为结构,类似于原始类型
- sql - 使用 BIGINT 时 Sqlite 整数溢出
- ionic-framework - 如何使用 PWA 访问设备上的照片?
- dax - 返回最大类别作为基于 DAX 的动态过滤条件中的度量
- c# - 如何在 DynamoDB 中最大化 WRU?
- azure - Azure Pipeline - 在测试应用程序中启动服务器
- lumen - 如何在流明应用程序中添加 laravel\passport 修复 app/Models/User.php 中的错误?