相关疑难解决方法(0)

AspNetCore 2.0身份-注入RoleManager的问题

我需要为ROLES创建CRUD操作。

我收到以下错误:

“无法解析类型为'Microsoft.AspNetCore.Identity.RoleManager`的服务”

那么,我该如何注入roleManager?

我正在使用ASP Net Core 2.0 + Identity 2.2.1

类ApplicationUser

 public class ApplicationUser : IdentityUser
    {
        [Key]
        public override string Id { get; set; }
        public bool Type { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

现在在Startup.cs中

        services.AddIdentity<ApplicationUser, IdentityRole<int>>()
        .AddUserStore<UserStore<ApplicationUser, IdentityRole<int>, ApplicationDbContext, int>>()
        .AddRoleStore<RoleStore<IdentityRole<int>, ApplicationDbContext, int>>()
        .AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)

控制者

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);
}
Run Code Online (Sandbox Code Playgroud)

因此,我收到错误消息:“无法解析类型为'Microsoft.AspNetCore.Identity.RoleManager`的服务。

c# asp.net-core asp.net-core-identity

5
推荐指数
1
解决办法
2359
查看次数

标签 统计

asp.net-core ×1

asp.net-core-identity ×1

c# ×1