gpt4 book ai didi

asp.net-core - Blazor WASM 中的授权 - 策略不起作用

转载 作者:行者123 更新时间:2023-12-05 04:51:25 24 4
gpt4 key购买 nike

因此,当我将基于策略的授权添加到 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"));
});

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

那么,如果我在启动时声明了政策,为什么上面标有政策的 Razor 页面会给我这个错误?

谢谢大家!!!

最佳答案

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

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

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

关于asp.net-core - Blazor WASM 中的授权 - 策略不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66922061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com