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

后端 未结 8 2004
你的背包
你的背包 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:17

    Not an expert, but ...

    There seemed to be no built in funcionality for this in Identity and I could not get it work from built in Roles also (it seems to not work with claims based Identity).

    So I ended up doing something like this:

    var users = context.Users        
        .Where(x => x.Roles.Select(y => y.Id).Contains(roleId))
        .ToList();
    
    • x.Roles.Select(y => y.Id) gets a list of all role ids for user x
    • .Contains(roleId) checks if this list of ids contains necessary roleId

提交回复
热议问题