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

搭建Identity后出现HTTP ERROR 404页面无法找到问题求助

解决Asp.NET 6集成Identity后出现的404错误

问题根源

集成Identity后出现404错误,核心问题有两个:

  • 未配置Razor Pages端点:AddDefaultIdentity依赖Razor Pages处理登录、注册等身份验证页面,程序中缺少Razor Pages的路由映射,导致Identity相关页面无法被找到。
  • 认证中间件语法错误:app.UseAuthentication();;存在多余分号,虽不直接触发404,但可能引发中间件管道的潜在异常。

修复方案

1. 添加Razor Pages路由映射

在Program.cs的路由配置区域,新增app.MapRazorPages();,确保Identity能正确找到对应的页面路由。修改后的代码片段如下:

app.UseRouting();
app.UseAuthentication(); // 移除多余的分号
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{area=User}/{controller=Home}/{action=Index}/{id?}");

// 添加Razor Pages路由映射
app.MapRazorPages();

app.Run();

2. 修正认证中间件语法

将app.UseAuthentication();;改为app.UseAuthentication();,移除多余分号,保证中间件管道正常执行。

3. 验证数据库配置

你的ApplicationDbContext继承IdentityDbContext的配置是正确的,需确保:

  • appsettings.json中的数据库连接字符串DefaultConnection配置正确。
  • 已执行迁移生成Identity相关数据表(若未执行,在包管理器控制台运行以下命令):
Add-Migration AddIdentityTables
Update-Database

额外检查项

  • 确认项目已安装Microsoft.AspNetCore.Identity.UI包(AddDefaultIdentity默认依赖此包,若缺失可通过NuGet安装)。
  • 若控制器或页面使用了[Authorize]特性,未登录用户会自动跳转至Identity登录页面,路由未配置时会触发404,需确保上述路由映射已添加。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 20:51:18