I'm having a problem using RoleManager in .NET 7 using Microsoft.AspNetCore.Identity (2.2.0) Everything compiles ok but when I go to get the roles from my JWT Middleware I get an exception when calling GetRolesAsync:
System.AggregateException: One or more errors occurred. (Store does not implement IUserRoleStore.) ---> System.NotSupportedException: Store does not implement IUserRoleStore. at Microsoft.AspNetCore.Identity.UserManager
1.GetUserRoleStore() at Microsoft.AspNetCore.Identity.UserManager
1.GetRolesAsync(TUser user)
DBContext/Entity Classes
public class MyDataContext : IdentityDbContext<MyTypeOfUser, IdentityRole, string>
{
public MyDataContext (DbContextOptions options)
: base(options) { }
...
}
public class MyTypeOfUser: IdentityUser
{
..
}
Program.cs
builder.Services.AddIdentity<MyTypeOfUser, IdentityRole>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<MyDataContext>();
JWT - GetJWTBearer Class
var userManager = context.HttpContext.RequestServices.GetService<UserManager<MyTypeOfUser>>();
var user = userManager.GetUserAsync(context.Principal).Result;
var roles = userManager.GetRolesAsync(user).Result;
I've seen a few similar questions here - namely: .NET Core 7: Store does not implement IUserRoleStore<TUser> and Store does not implement IUserRoleStore<TUser> ASP.NET Core Identity
However I've implemented all fixes I can see from their answers that I can whilst using .NET 7. The database seems to have created the right tables... I'm flumoxed!