0

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.UserManager1.GetUserRoleStore() at Microsoft.AspNetCore.Identity.UserManager1.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!

Chris Nevill
  • 5,922
  • 11
  • 44
  • 79

1 Answers1

0

OK I had missed something. I also had this in program.cs... added sometime earlier.

IdentityEntityFrameworkBuilderExtensions.AddEntityFrameworkStores<AuctionDataContext>(        
        AddIdentityFrameworkCore(builder.Services)        
    );

Removing it seems to have resolved the issue.

Chris Nevill
  • 5,922
  • 11
  • 44
  • 79