'I got this error "Unable to resolve service for type AspNetCore.Identity.SignInManager" when I use ApplicationUser instead IdentityUser

I have a .NET Core 3.1 Web Api and I created an ApplicationUser class that inherits from IdentityUser because I need to register some others properties. But when I attempt to register a new user I get the following error:

Unable to resolve service for type 'Microsoft.AspNetCore.Identity.SignInManager[MyProject.Api.ApplicationUser]' while attempting to activate 'MyProject.Api.Controllers.AuthController'.

Here's the IdentityConfig class that set the dependencys on Startup class:

public static class IdentityConfig
{
    public static IServiceCollection AddIdentityConfig(this IServiceCollection services,
        IConfiguration configuration)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
        
        services.AddDefaultIdentity<ApplicationUser>()
            .AddRoles<IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddErrorDescriber<IdentityMensagensPortugues>()
            .AddDefaultTokenProviders();

        var appSettingsSection = configuration.GetSection("AppSettings");
        services.Configure<AppSettings>(appSettingsSection);

        var appSettings = appSettingsSection.Get<AppSettings>();
        var key = Encoding.ASCII.GetBytes(appSettings.Secret);

        services.AddAuthentication(x =>
        {
            x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(x =>
        {
            x.RequireHttpsMetadata = true;
            x.SaveToken = true;
            x.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(key),
                ValidateIssuer = true,
                ValidateAudience = true,
                ValidAudience = appSettings.ValidoEm,
                ValidIssuer = appSettings.Emissor
            };
        });

        return services;
    }
}

Here's the ApplicationDbContext class:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> {
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
}

Here's the Controller class:

public class AuthController : MainController
{
    private readonly SignInManager<ApplicationUser> _signInManager;
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly AppSettings _appSettings;
    private readonly ILogger _logger;

    public AuthController(INotificator notificator, 
                          SignInManager<ApplicationUser> signInManager, 
                          UserManager<ApplicationUser> userManager, 
                          IOptions<AppSettings> appSettings,
                          IUser user, ILogger<AuthController> logger) : base(notificator, user)
    {
        _signInManager = signInManager;
        _userManager = userManager;
        _logger = logger;
        _appSettings = appSettings.Value;
    }

    [HttpPost("new-account")]
    public async Task<ActionResult> Register(RegisterUserViewModel registerUser)
    {
        if (!ModelState.IsValid) return CustomResponse(ModelState);

        ApplicationUser user = new ApplicationUser {
            UserName = registerUser.Email,
            Email = registerUser.Email,
            Cpf = registerUser.Cpf,
            ZipCode = registerUser.ZipCode,
            Gender = registerUser.Gender,
            EmailConfirmed = true
        };

        var result = await _userManager.CreateAsync(user, registerUser.Password);
        
        if (result.Succeeded)
        {
            await _signInManager.SignInAsync(user, false);
            return CustomResponse(await GerarJwt(user.Email));
        }
        foreach (var error in result.Errors)
        {
            NotifyError(error.Description);
        }

        return CustomResponse(registerUser);
    }

    [HttpPost("enter")]
    public async Task<ActionResult> Login(LoginUserViewModel loginUser)
    {
        if (!ModelState.IsValid) return CustomResponse(ModelState);

        var result = await _signInManager.PasswordSignInAsync(loginUser.Email, loginUser.Password, false, true);

        if (result.Succeeded)
        {
            _logger.LogInformation("User "+ loginUser.Email +" successfully logged in");
            return CustomResponse(await GerarJwt(loginUser.Email));
        }
        if (result.IsLockedOut)
        {
            NotifyError("User temporarily blocked by invalid attempts");
            return CustomResponse(loginUser);
        }

        NotificarErro("User or Password incorrect");
        return CustomResponse(loginUser);
    }
}


Solution 1:[1]

make sure your ApplicationDbContext has ApplicationUser and IdentityRole, cause you have defined that way in your startup class.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole, string>
    {
            public ApplicationDbContext (DbContextOptions<ApplicationDbContext> options) : base(options)
            {
            }
    }

Solution 2:[2]

I cannot point where exactly where to fix, but I can tell that you are trying to inject SignInManager<ApplicationUser> into your controller. Suggesting that somewhere in your startup the manager is not being registered with your user.

If you try to inject SignInManager<IdentityUser> does it work? If it does you just have to find a way to configure DI with your custom user.

Solution 3:[3]

Make sure you have called IdentityConfig.AddIdentityConfig in Startup.cs or Program.cs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Asif Rahman
Solution 2 Arley Pádua
Solution 3 Bron Davies