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

如何在.NET Identity Framework中设置Login.cshtml为启动页

问题:设置.NET Identity应用启动直接加载Login页面

我正在学习.NET Identity Framework。Identity Framework的默认页面无需控制器操作即可运行,没法像访问关联控制器的自定义页面那样直接访问这些页面。我希望应用启动时直接加载位于Areas/Identity/Pages/Account/目录下的Login.cshtml页面。

以下是我的Program.cs代码:

using IdentityStudy.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddDbContext<StudyDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("ConString")));

builder.Services.AddDefaultIdentity<IdentityUser>().AddDefaultTokenProviders().
    AddRoles<IdentityRole>().
    AddEntityFrameworkStores<StudyDbContext>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    endpoints.MapFallbackToPage("/Areas/Identity/Pages/Account/Login", "/Identity");
});

app.Run();

解决方案

你当前的路由配置和中间件顺序存在问题,按以下方式修改Program.cs即可实现启动直接加载Login页面:

修改后的完整代码

using IdentityStudy.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;

var builder = WebApplication.CreateBuilder(args);

// 添加服务到容器
builder.Services.AddControllersWithViews();

builder.Services.AddDbContext<StudyDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("ConString")));

// 配置Identity服务,添加AddDefaultUI确保默认页面路由生效
builder.Services.AddDefaultIdentity<IdentityUser>()
    .AddDefaultTokenProviders()
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<StudyDbContext>()
    .AddDefaultUI(); // 新增此方法,注册Identity默认页面的路由

var app = builder.Build();

// 配置HTTP请求管道
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();

app.UseRouting();

// 必须先启用身份验证,再启用授权,中间件顺序不能错
app.UseAuthentication();
app.UseAuthorization();

// 映射Razor Pages,无需在UseEndpoints中重复配置
app.MapRazorPages();

// 设置回退路由到Login页面,路径格式为区域内的相对路径 + 区域名称
app.MapFallbackToPage("/Account/Login", "/Identity");

app.Run();

关键修改点说明

  • 添加AddDefaultUI():此方法负责注册Identity默认页面的路由规则,确保Login等页面能被正确访问。
  • 调整中间件顺序:UseAuthentication必须放在UseAuthorization之前,否则身份验证逻辑无法生效,会导致Login页面访问异常。
  • 修正MapFallbackToPage路径:第一个参数只需传入Identity区域内的页面相对路径(无需写完整的Areas/Identity/Pages前缀),第二个参数指定区域名称/Identity。

额外优化:强制未认证用户跳转Login页面

如果希望所有未登录的用户访问任何页面都自动跳转到Login,可以添加全局授权策略:

// 在builder.Services.AddControllersWithViews()之后添加
builder.Services.AddAuthorization(options =>
{
    options.FallbackPolicy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .Build();
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 02:15:00