首页 > 解决方案 > 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"));
            });

当我查看用户的“声明”时,没有显示来自数据库的声明。他们在那里。

那么,为什么,如果我在启动时声明了策略,那么标有上述策略的剃须刀页面是否会给我这个错误?

谢谢大家!!!

标签: asp.net-coreauthorizationclaims-based-identityblazor-webassembly

解决方案


我通过在 Main 函数中添加以下客户端的 Program.cs 来使其工作。

builder.services.AddAuthorizationCore(o =>
{
  o.AddPolicy("SuperAdmin", policy => policy.RequireClaim("SuperAdmin"));
  o.AddPolicy("CountyAdmin", policy => policy.RequireClaim("CountyAdmin"));
});

您在服务器上设置的授权。这是为客户做的。


推荐阅读