简体   繁体   English

Blazor WASM 中的授权 - 策略不起作用

[英]Authorization in Blazor WASM - policies don't work

So when I add policy-based authorization to the Startup.cs file and then add AuthorizeView to my Blazor page, I get an error:因此,当我将基于策略的授权添加到 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

But in my Startup:但是在我的启动中:

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

When I take a look at the 'Claims' for the user, the claims from the DB don't show.当我查看用户的“声明”时,数据库中的声明没有显示。 They are there though.他们在那里。

So, why, if I've declared the policies in startup does the razor page marked with the Policies above give me this error?那么,如果我在启动时声明了策略,为什么标有上述策略的 razor 页面会给我这个错误?

Thanks all!!!谢谢大家!!!

I got it to work by adding the following the the Client's Program.cs in the Main function.我通过在 Main function 中添加以下客户端的 Program.cs 来使其工作。

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

What you had setup authorization on the server.您在服务器上设置的权限。 This does it for the client.这是为客户做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM