[英]How to find all users in a role with extended properties value Asp.net Identity 2
我在具有在ApplicationUser類中作為OrganisationId擴展屬性的項目中使用身份。
我正在使用ApplicationUserManager讀取用戶詳細信息。
這是我的ApplicationUser類:
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類中注入ApplicationUserManager對象。
然后,用戶管理器對象為我提供了一種方法來查找用戶是否擔任角色。
var isUserAdmin = await _userManager.IsInRoleAsync(userId, adminRoleName);
但是我需要的是一種找到組織中具有管理員權限的所有用戶的方法。
像這樣:
_userManager.Users.Where(u=>u.OrganisationId=1 && u.Roles.Contains(adminRole))
但這不起作用,因為角色是ApplicationUserRole的集合。
知道如何管理組織中的所有管理員用戶嗎?
只需使用:
u.Roles.Any(m => m.RoleId == adminRole.Id)
代替。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.