如何查找具有扩展属性值 Asp.net 身份 2 的角色中的所有用户

How to find all users in a role with extended properties value Asp.net Identity 2

我在一个项目中使用 Identity,该项目在 ApplicationUser class 中具有扩展属性作为 OrganisationId。

我正在使用 ApplicationUserManager 读取用户详细信息。

这是我的 ApplicationUser class:

public class ApplicationUser
    : IdentityUser<int, ApplicationUserLogin,
        ApplicationUserRole, ApplicationUserClaim>, IUser<int>
{
    public async Task<ClaimsIdentity>
        GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

        userIdentity.AddClaim(new Claim("OrganisationId", OrganisationId.ToString()));
        return userIdentity;
    }

    public int OrganisationId { get; set; }
}

然后在 AccountController class 中注入 ApplicationUserManager 对象。

然后用户管理器对象为我提供了一种查找用户是否在角色中的方法。

 var isUserAdmin = await _userManager.IsInRoleAsync(userId, adminRoleName);

但我需要的是一种找到组织中所有拥有管理员权限的用户的方法。

像这样:

_userManager.Users.Where(u=>u.OrganisationId=1 && u.Roles.Contains(adminRole))

但这不起作用,因为 Roles 是 ApplicationUserRole 的集合。

知道如何设法让组织中的所有管理员用户吗?

只需使用:

u.Roles.Any(m => m.RoleId == adminRole.Id)

相反。