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

.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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 00:45:43