Asp.net core 重写SignInManager在启动时引发错误
我试图用CustomSignManager覆盖SignInManager,但在startup类中注册它时,我遇到了一个错误 System.InvalidOperationException:类型CustomSignInManager`1必须从UserManager派生。 位于Microsoft.AspNetCore.Identity.IdentityBuilder.addUserManagerUserManager 下面是我用来注册CustomSignInManager的代码Asp.net core 重写SignInManager在启动时引发错误,asp.net-core,asp.net-identity-3,Asp.net Core,Asp.net Identity 3,我试图用CustomSignManager覆盖SignInManager,但在startup类中注册它时,我遇到了一个错误 System.InvalidOperationException:类型CustomSignInManager`1必须从UserManager派生。 位于Microsoft.AspNetCore.Identity.IdentityBuilder.addUserManagerUserManager 下面是我用来注册CustomSignInManager的代码 s
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddUserManager<CustomSignInManager<ApplicationUser>>();
services.AddScoped<SignInManager<ApplicationUser>, CustomSignInManager<ApplicationUser>>();
services.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders()
.AddUserManager();
services.addScope();
在CustomSignInManager上,我只是返回任何基类,以简化这里的操作
public class CustomSignInManager<TUser> : SignInManager<TUser> where TUser : class
{
public CustomSignInManager(UserManager<TUser> userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory<TUser> claimsFactory, IOptions<IdentityOptions> optionsAccessor, ILogger<SignInManager<TUser>> logger, ApplicationDbContext dbContext, IAuthenticationSchemeProvider schemes, IUserConfirmation<TUser> confirmation)
: base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes, confirmation)
{
}
}
public类CustomSignInManager:SignInManager其中TUser:class
{
公共CustomSignInManager(UserManager UserManager、IHttpContextAccessor contextAccessor、IUserClaimsPrincipalFactory claimsFactory、IOOptions Accessor、ILogger记录器、ApplicationDbContext dbContext、IAAuthenticationSchemeProvider方案、IUserConfirmation确认)
:base(userManager、contextAccessor、claimsFactory、OptionAccessor、logger、schemes、confirmation)
{
}
}
System.InvalidOperationException:类型CustomSignInManager`1必须从UserManager派生
在您的代码中,我们可以发现您希望在Startup
类的ConfigureServices
方法中使用自定义SignInManager配置Identity service,但是您调用.AddUserManager方法来添加自定义SignInManager,这会导致此错误
.AddUserManager()
要修复此问题,可以尝试调用.AddSignInManager()
方法来添加自定义SignInManager,而不是使用上述代码