System.NotSupportedException: Store does not implement IUserRoleStore<TUser>

15 Views Asked by At

I'm trying to retrieve the roles of a specific user in my application. Here's the relevant code: Asp.net 8 AuthController.cs

private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly TokenService _tokenService;

public AuthController(
    UserManager<ApplicationUser> userManager,
    SignInManager<ApplicationUser> signInManager,
    TokenService tokenService)
{
    _userManager = userManager;
    _signInManager = signInManager;
    _tokenService = tokenService;
}

public async Task<IActionResult> Login(LoginDto model)
{
    var user = await _userManager.FindByNameAsync(model.UserName);

    if (user != null && await _userManager.CheckPasswordAsync(user, model.Password))
    {
        var roles = await _userManager.GetRolesAsync(user);
        var token = _tokenService.GenerateToken(user, roles);

        return Ok(new { Token = token });
    }

    return Unauthorized();
}

However, it's failing at _userManager.GetRolesAsync(user).

I've tried setting up roles as follows:

Program.cs

builder.Services.AddIdentityCore<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddSignInManager()
    .AddDefaultTokenProviders();

ApplicationUser.cs

public class ApplicationUser : IdentityUser
{
    [MaxLength(256)] // This is for local validation in EF Core
    [Column(TypeName = "character varying(256)")] // This defines the column type in the database
    public string FullName { get; set; }
}

But it's still not working. Any ideas?

0

There are 0 best solutions below