AspNetCore 2.0 Identification - RoleManager Injection Issues

I need to create CRUD operations for ROLES.

I get the following error:

"Unable to enable service for type" Microsoft.AspNetCore.Identity.RoleManager "

So how can I introduce roleManager?

I am using asp net core 2.0 + identity 2.2.1

Class ApplicationUser

 public class ApplicationUser : IdentityUser
    {
        [Key]
        public override string Id { get; set; }
        public bool Type { get; set; }
    }

Now in Startup.cs

        services.AddIdentity<ApplicationUser, IdentityRole<int>>()
        .AddUserStore<UserStore<ApplicationUser, IdentityRole<int>, ApplicationDbContext, int>>()
        .AddRoleStore<RoleStore<IdentityRole<int>, ApplicationDbContext, int>>()
        .AddDefaultTokenProviders();

controller

private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityUser> _roleManager;

public RolesController(UserManager<ApplicationUser> userManager, RoleManager<IdentityUser> roleManager)
{
    _userManager = userManager;
    _roleManager = roleManager; 
}


public IActionResult Index()
{
    return View(_roleManager.Roles);
}

So, I get an error: "Could not resolve service for type" Microsoft.AspNetCore.Identity.RoleManager`.

+4
source share
1 answer

.AddRoleManager < > AddRoleStore < > . , . , : AddEntityFrameworkStores

services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<YourContext>()
+1

Source: https://habr.com/ru/post/1688308/


All Articles