Asp.net

如何讓所有使用者成為角色(Microsoft ASP.NET Identity EntityFramework 2.0.0-beta1)?

  • September 17, 2021

我剛剛更新到 ASP.NET Identity EntityFramework 2.0.0-beta1,我的 Roles 類出現編譯錯誤。也許有人可以給我一些線索如何讓所有使用者獲得特定角色?

這是非常接近的東西,當我使用 Intellisense 瀏覽時,我就快到了,但需要提示 :-)。

這是更新前的工作方式:

user.Roles.First().Role.Name.Equals("Admins")

它僅暴露在 EF 實現層上,因此:

roleManager.FindByName("Admins").Users

接受的答案返回CustomUserRoles。如果您正在尋找 的列表ApplicationUsers,請嘗試:

public IList<ApplicationUser> GetApplicationUsersInRole(string roleName)
{
    var selectedUserIds = from role in roleManager.Roles
                          where role.Name == roleName
                          from user in role.Users
                          select user.UserId;
   // this _users comes from the ApplicationDbContext.ApplicationUser
    return _users.Where(applicationUser => selectedUserIds.Contains(applicationUser.Id)).ToList();
}

引用自:https://stackoverflow.com/questions/22209093