.NET 5升级至7后登录失败:SignInManager服务无法解析
问题解决:无法解析SignInManager服务
错误原因
你的AuthController依赖SignInManager<software.API.Models.User>,但依赖注入容器中未注册该服务。这是因为你使用了AddIdentityCore<User>方法,它仅注册Identity的核心基础服务,不会自动包含SignInManager——从.NET 5升级到7的过程中,Identity的服务注册逻辑未发生变化,但如果之前依赖完整的AddIdentity(会自动注册SignInManager),切换为AddIdentityCore后就会缺失该服务。
修复步骤
修改IdentityServiceExtensions中的AddIdentityCore链式调用,添加AddSignInManager来注册所需服务:
services.AddIdentityCore<User>(opt => { opt.Password.RequireNonAlphanumeric = false; opt.Lockout.MaxFailedAccessAttempts = 3; opt.Password.RequireDigit = true; opt.Password.RequiredLength = 8; opt.Password.RequireNonAlphanumeric = false; opt.Password.RequireUppercase = true; }) .AddRoles<Role>() .AddRoleManager<RoleManager<Role>>() .AddRoleValidator<RoleValidator<Role>>() .AddEntityFrameworkStores<DataContext>() // 添加这行代码注册SignInManager .AddSignInManager<SignInManager<User>>();
额外检查项
- 确认
User类继承自Microsoft.AspNetCore.Identity.IdentityUser(或带主键泛型的版本,比如IdentityUser<int>) - 确认
Role类继承自Microsoft.AspNetCore.Identity.IdentityRole(或对应主键版本) - 确认
DataContext继承自IdentityDbContext<User, Role, int>(根据你的主键类型调整泛型参数)
内容的提问来源于stack exchange,提问作者GolfBravo
相关产品推荐
相关产品推荐

