覆盖 SignInManager 在启动时抛出错误
Overriding SignInManager throwing an error on startup
我试图用 CustomSignManager 覆盖 SignInManager,但在启动时注册它时 class 我收到错误
System.InvalidOperationException:类型 CustomSignInManager`1 必须派生自 UserManager。
在 Microsoft.AspNetCore.Identity.IdentityBuilder.AddUserManagerTUserManager
下面是我用来注册 CustomSignInManager 的代码
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddUserManager<CustomSignInManager<ApplicationUser>>();
services.AddScoped<SignInManager<ApplicationUser>, CustomSignInManager<ApplicationUser>>();
在 CustomSignInManager 上,我只是返回任何基础 class 返回以简化此处。
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)
{
}
}
System.InvalidOperationException: Type CustomSignInManager`1 must derive from UserManager<ApplicationUser>.
在您的代码中,我们可以发现您想在 Startup
class 的 ConfigureServices
方法中使用自定义 SignInManager 配置身份服务,但您调用 .AddUserManager<TUserManager> method
添加自定义 SignInManager,这会导致此错误。
.AddUserManager<CustomSignInManager<ApplicationUser>>()
要修复它,您可以尝试调用 .AddSignInManager<CustomSignInManager<ApplicationUser>>()
方法来添加您的自定义 SignInManager 而不是使用上面的代码。
我试图用 CustomSignManager 覆盖 SignInManager,但在启动时注册它时 class 我收到错误
System.InvalidOperationException:类型 CustomSignInManager`1 必须派生自 UserManager。 在 Microsoft.AspNetCore.Identity.IdentityBuilder.AddUserManagerTUserManager
下面是我用来注册 CustomSignInManager 的代码
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddUserManager<CustomSignInManager<ApplicationUser>>();
services.AddScoped<SignInManager<ApplicationUser>, CustomSignInManager<ApplicationUser>>();
在 CustomSignInManager 上,我只是返回任何基础 class 返回以简化此处。
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)
{
}
}
System.InvalidOperationException: Type CustomSignInManager`1 must derive from UserManager<ApplicationUser>.
在您的代码中,我们可以发现您想在 Startup
class 的 ConfigureServices
方法中使用自定义 SignInManager 配置身份服务,但您调用 .AddUserManager<TUserManager> method
添加自定义 SignInManager,这会导致此错误。
.AddUserManager<CustomSignInManager<ApplicationUser>>()
要修复它,您可以尝试调用 .AddSignInManager<CustomSignInManager<ApplicationUser>>()
方法来添加您的自定义 SignInManager 而不是使用上面的代码。