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

Unable to resolve service for type 'Microsoft.AspNetCore.Identity.SignInManager'


When I inject SignInManager in ASP.NET Core 3.1, I get the exception message

Unable to resolve service for type 'Microsoft.AspNetCore.Identity.SignInManager'

My Account Controller

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>>();

My User class

public class User : IdentityUser
{
    //public string FirstName { get; set; }
    //public string LastName { get; set; }
}

Solution

  • You should use the AddSignInManager extension on IdentityBuilder.

    Assuming that you've replaced IdentityUser with User elsewhere in your project, you can use .AddSignInManager<SignInManager<User>>() like below.

    services.AddIdentityCore<User>(opt =>
    {
        opt.User.RequireUniqueEmail = true;
    })
    .AddRoles<IdentityRole>()
    .AddClaimsPrincipalFactory<UserClaimsPrincipalFactory<User,IdentityRole>>()
    .AddSignInManager<SignInManager<User>>()
    .AddEntityFrameworkStores<HISDbContext>()
    .AddDefaultTokenProviders();
    

    AddSignInManager Documentation