Identity Core 2 IdentityRole是否缺少用户的定义?

时间:2017-11-16 16:29:34

标签: identity asp.net-core-2.0

我是否遗漏了某些内容,或者是否已从Identity Core 2中的AssetsDownloadingProcessFactory中删除了用户的定义?

我正在使用asp.net core 2,我需要计算每个角色的用户数。这在Core 1中运行良好,具有以下标准代码

IdentityRole

在我的使用Core 2的应用程序中,行public class ApplicationRoleController : Controller { private readonly RoleManager<ApplicationRole> roleManager; public ApplicationRoleController(RoleManager<ApplicationRole> roleManager) { this.roleManager = roleManager; } [HttpGet] public IActionResult Index() { List<ApplicationRoleListViewModel> model = new List<ApplicationRoleListViewModel>(); model = roleManager.Roles.Select(r => new { RoleName = r.Name, Id = r.Id, Description = r.Description, NumberOfUsers = r.Users.Count }).ToList() .Select(r => new ApplicationRoleListViewModel { RoleName = r.RoleName, Id = r.Id, Description = r.Description, NumberOfUsers = r.NumberOfUsers }).ToList(); return View(model); } ,其中r派生自类NumberOfUsers = r.Users.Count,错误为&#34; ApplicationRole不包含用户定义&#34; ApplicationRole继承自ApplicationRole

IdentityRole

1 个答案:

答案 0 :(得分:1)

您可以使用此

var admins = _userManager.GetUsersInRoleAsync("Admin").Result;
var number = admins.Count;

Core 2的问题显然是他们放弃了导航属性 AspNetUsers AspNetUserRoles AspNetRoles。不知道为什么。老实说非常令人沮丧。据说你可以重新实施它们,但到目前为止我没有运气。我在这里试图找到一种方法来列出所有用户的所有角色。 我认为上面的代码就是你所需要的。

此外_userManager也是在控制器中导入的

private readonly UserManager<ApplicationUser> _userManager;


    public MyControllerController(DBContext context, UserManager<ApplicationUser> userManager)
    {
        this.context = context;
        _userManager = userManager; ;
    }