请教一下 Blazor web app (.net 9.0) 角色授权的 问题

929Free 661 信誉分
2025-04-14T06:13:08.6566667+00:00

哈喽,工程师们好,我在 ****.razor 页面里 加入 @attribute [Authorize(Roles = "超级管理员")] 语句,

当登录拥有该角色权限的用户 访问 ****.razor 页面时,是正常的, 当登录的用户不拥有 超级管理员 该角色时, 页面就报以下错误了:

An unhandled exception occurred while processing the request.

InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).

Program.cs:

builder.Services.AddAuthentication();
builder.Services.AddCascadingAuthenticationState();

app.UseAuthentication();
app.UseAuthorization();

我的需求是当没有该角色的用户在访问此页面时, 应该自动跳转到 login.razor 页面, 请问我该如何设置呢?

开发人员技术 | .NET | Blazor

你的答案

提问者可以将答案标记为“已接受”,审查方可以将答案标记为“已推荐”,这有助于用户了解答案是否解决了提问者的问题。