You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Blazor Server应用中自定义‘未授权’提示信息?

自定义Blazor Server未授权提示的两种方法

针对你在Blazor Server(.NET 6、ASP.NET Core Identity)中遇到的默认未授权提示自定义需求,以下是两种可行方案:

方法一:单个页面内自定义(使用AuthorizeView)

如果仅需给Index页面单独设置自定义提示,可以移除页面类上的[Authorize]特性(或保留以强化页面级授权),改用AuthorizeView组件包裹页面内容,灵活定义未授权状态的显示内容:

修改Index.razor:

@page "/"
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize] <!-- 可选:保留特性确保页面整体不可被未授权用户直接访问 -->

<PageTitle>Index</PageTitle>

<AuthorizeView>
    <Authorized>
        <h1>Hello, world!</h1>
        Welcome to your new app.
        <SurveyPrompt Title="How is Blazor working for you?" />
    </Authorized>
    <NotAuthorized>
        <!-- 自定义未授权内容 -->
        <div class="alert alert-danger mt-4">
            <h3>抱歉,您无权限访问此页面</h3>
            <p>请登录后重试,或联系管理员获取访问权限。</p>
            <a href="Identity/Account/Login" class="btn btn-primary">前往登录</a>
        </div>
    </NotAuthorized>
</AuthorizeView>

方法二:全局统一自定义未授权组件

如果希望所有带[Authorize]特性的页面都使用统一的未授权提示,可以通过替换框架默认的授权失败处理逻辑实现:

步骤1:创建自定义未授权组件

新建Pages/Shared/Unauthorized.razor:

<div class="container mt-5">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="card border-danger">
                <div class="card-header bg-danger text-white">
                    <h4>未授权访问</h4>
                </div>
                <div class="card-body">
                    <p>您当前账户无权限访问该页面,请检查登录状态或联系系统管理员。</p>
                    <div class="d-flex gap-2">
                        <a href="Identity/Account/Login" class="btn btn-primary">登录账户</a>
                        <a href="/" class="btn btn-secondary">返回首页</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

步骤2:配置全局授权处理逻辑

在Program.cs中添加自定义授权结果处理器的注册:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Policy;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Identity;
using BlazorServerApp.Data;

var builder = WebApplication.CreateBuilder(args);

// 原有服务配置(保留)
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();

// 注册自定义授权结果处理器
builder.Services.AddScoped<IAuthorizationMiddlewareResultHandler, CustomAuthorizationMiddlewareResultHandler>();

var app = builder.Build();

// 原有中间件配置(保留)
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");

app.Run();

步骤3:实现自定义授权结果处理器

创建CustomAuthorizationMiddlewareResultHandler.cs:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Policy;
using Microsoft.AspNetCore.Components;
using System.Threading.Tasks;

public class CustomAuthorizationMiddlewareResultHandler : IAuthorizationMiddlewareResultHandler
{
    private readonly AuthorizationMiddlewareResultHandler _defaultHandler = new();

    public async Task HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
    {
        if (authorizeResult.Forbidden && context.Request.Path.StartsWithSegments("/_blazor"))
        {
            // 重定向到自定义未授权页面
            var navigationManager = context.RequestServices.GetRequiredService<NavigationManager>();
            navigationManager.NavigateTo("/Unauthorized", forceLoad: true);
            return;
        }

        // 其他授权结果使用框架默认处理
        await _defaultHandler.HandleAsync(next, context, policy, authorizeResult);
    }
}

步骤4:添加必要命名空间(可选)

在_Imports.razor中添加授权相关命名空间,避免重复引用:

@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization

内容的提问来源于stack exchange,提问作者jh k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 06:10:42