.NET 6中添加AddDefaultIdentity后出现页面无法找到的问题排查求助
.NET 6中添加AddDefaultIdentity后出现页面无法找到的问题排查求助
我正在开发一个.NET 6应用,最近在添加默认身份认证(Default Identity)时碰到了棘手的问题。具体情况是这样的:我先单独建了一个测试项目,在里面用脚手架搭建好了Identity功能,这个测试项目运行完全正常;但当我把测试项目里所有脚手架生成的内容复制粘贴到我的实际项目后,访问页面就出现了**"This localhost page can't be found"**的错误。
以下是我的Program.cs代码:
using Microsoft.EntityFrameworkCore; using Thriftshop.DataAccess.Repository.IRepository; using Microsoft.AspNetCore.Identity; using Thriftshop.DataAccess; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); 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<IUnitOfWork, UnitOfWork>(); //builder.Services.AddRazorPages().AddRazorRuntimeCompilation(); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); ; app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{area=Customer}/{controller=Home}/{action=Index}/{id?}"); app.Run();
我发现只要把AddDefaultIdentity那一行去掉,项目就能正常运行。所有相关NuGet包的版本都是6.0.12,我已经尝试了这些排查步骤:
- 更换过包版本,怀疑是Identity包存在bug
- 检查了DbContext文件,确认没有配置问题
- 在
AddDefaultIdentity中添加了options => options.SignIn.RequireConfirmedAccount = true的配置项
想请教各位,这个问题的根源可能是什么?
备注:内容来源于stack exchange,提问作者dimitri
相关产品推荐
相关产品推荐

