当我在ASP.NET Core 3.1中注入SignInManager时,我得到异常消息
无法解析类型为"Microsoft.AspNetCore.Identity.SignInManager"的服务
我的账户管理员
private readonly UserManager<User> userManager;
private readonly SignInManager<User> signInManager;
public AccountController(UserManager<User> _userManager, SignInManager<User> _signInManager)
{
this.userManager = _userManager;
this.signInManager = _signInManager;
}
My Startup.cs
services.AddIdentityCore<User>(opt =>
{
opt.User.RequireUniqueEmail = true;
})
.AddRoles<IdentityRole>()
.AddClaimsPrincipalFactory<UserClaimsPrincipalFactory<User,IdentityRole>>()
.AddEntityFrameworkStores<HISDbContext>()
.AddDefaultTokenProviders();
services.AddAutoMapper(typeof(Startup));
services.AddDbContext<HISDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("HISConn"))
);
//services.AddScoped<UserManager<User>>();
我的用户类
public class User : IdentityUser
{
//public string FirstName { get; set; }
//public string LastName { get; set; }
}
您应该在IdentityBuilder
上使用AddSignInManager
扩展名。
假设您已经在项目的其他地方用User
替换了IdentityUser
,那么您可以像下面这样使用.AddSignInManager<SignInManager<User>>()
。
services.AddIdentityCore<User>(opt =>
{
opt.User.RequireUniqueEmail = true;
})
.AddRoles<IdentityRole>()
.AddClaimsPrincipalFactory<UserClaimsPrincipalFactory<User,IdentityRole>>()
.AddSignInManager<SignInManager<User>>()
.AddEntityFrameworkStores<HISDbContext>()
.AddDefaultTokenProviders();
AddSignInManager
文档