Search code examples
asp.net-coreasp.net-identity-3

Overriding SignInManager throwing an error on startup


I am trying to override the SignInManager with CustomSignManager but while registering it at the startup class I am getting an error

System.InvalidOperationException: Type CustomSignInManager`1 must derive from UserManager. at Microsoft.AspNetCore.Identity.IdentityBuilder.AddUserManagerTUserManager

Below is the of code I am using to register CustomSignInManager

        services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders()
                .AddUserManager<CustomSignInManager<ApplicationUser>>(); 


services.AddScoped<SignInManager<ApplicationUser>, CustomSignInManager<ApplicationUser>>();

On the CustomSignInManager I am just returning whatever base class is returning to make this simple here.

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)
    {
    }
}

Solution

  • System.InvalidOperationException: Type CustomSignInManager`1 must derive from UserManager<ApplicationUser>.

    In you code, we can find that you'd like to configure Identity service with a custom SignInManager in ConfigureServices method of Startup class, but you call .AddUserManager<TUserManager> method to add a custom SignInManager, which cause this error.

    .AddUserManager<CustomSignInManager<ApplicationUser>>()

    To fix it, you can try to call .AddSignInManager<CustomSignInManager<ApplicationUser>>() method to add your custom SignInManager instead of using your above code.