Get list of users with assigned roles in asp.net identity 2.0

后端 未结 8 1994
你的背包
你的背包 2020-12-23 21:13

I have a drop down list box which lists roles. I want to get the list of users having that role. I mean list of users that are in \"Administrator\" role or \"CanEdit\" role.

8条回答
  •  自闭症患者
    2020-12-23 21:21

    Using the RoleManager gives you this solution:

    if(roleManager.RoleExists("CanEdit"))
    {
        var idsWithPermission = roleManager.FindByName("CanEdit").Users.Select(iur => iur.Id);
        var users = db.Users.Where(u => idsWithPermission.Contains(u.Id));
    }
    

    I'd be interested to hear if this was better or worse than the other solutions here.

提交回复
热议问题