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

929Free 641 信誉分
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 页面, 请问我该如何设置呢?

Blazor
Blazor
一个免费的开源 Web 框架,使开发人员能够使用 Microsoft 开发的 C# 和 HTML 创建 Web 应用。
35 个问题
{count} 票

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。