如何从 ASP.NET Identity ApplicationGroupRoles table 获取角色

How to obtain roles from ASP.NET Identity ApplicationGroupRoles table

我可以从 AspNetUserRoles table 的先前版本 ASP.NET Identity 中检索用户的所有角色,如下所示:

ApplicationUser user = UserManager.FindByName(model.UserName);
string userId = user != null ? user.Id : null;
var roles = userId != null ? UserManager.GetRoles(userId) : null;
if (roles != null)
{
    foreach (var item in roles)
    {
        //Assign user roles
        UserManager.AddToRole(userId, item);
    }
}

但是,由于角色是通过 ApplicationGroupRolesApplicationUserGroups table 分配给用户的,因此 UserManager.GetRoles(userId) 方法不起作用,因为它仅从 AspNetUserRoles table 检索角色。那么,如何设法检索给定用户的角色,即首先查看 ApplicationUserGroups 然后 ApplicationGroupRoles table? 或者是使用 sql 命令 检索它们的唯一方法?任何帮助,将不胜感激。

虽然我已经发布了另一种查找角色的方法,但是您的代码我们没问题,您是否在 table

中输入了任何角色

AspNetRoles

并在 table

中创建了用户和角色之间的关系

AspNetUserRoles

要查找特定用户的所有角色,您可以使用以下代码

    RoleBasedSecurity.Models.ApplicationDbContext context = new ApplicationDbContext();
    ApplicationUser au = context.Users.First(u => u.UserName == "admin@admin.com");

    foreach (IdentityUserRole role in au.Roles)
    {
        string name = role.RoleId;
        string RoleName = context.Roles.First(r => r.Id == role.RoleId).Name;
    }

创建角色的代码将这些行写在 protected override void Seed(RoleBasedSecurity.Models.ApplicationDbContext context) in cofiguration.cs

    if (!context.Roles.Any(r => r.Name == "User"))
    {
        var store = new RoleStore<IdentityRole>(context);
        var manager = new RoleManager<IdentityRole>(store);
        var role = new IdentityRole { Name = "User" };

        manager.Create(role);
    }

将用户附加到角色的代码 在 protected override void Seed(RoleBasedSecurity.Models.ApplicationDbContext context) in cofiguration.cs[= 中写入这些行29=]

    if (!context.Users.Any(u => u.UserName == "admin@admin.com"))
    {
        var store = new UserStore<ApplicationUser>(context);
        var manager = new UserManager<ApplicationUser>(store);
        var user = new ApplicationUser { UserName = "admin@admin.com", Email = "admin@admin.com" };
        manager.Create(user, "password");
        manager.AddToRole(user.Id, "Admin");
    }

最后我使用以下语句来获取相关值:

ApplicationGroupManager gm = new ApplicationGroupManager();
string roleName = RoleManager.FindById("").Name; //Returns Role Name by using Role Id 
var userGroupRoles = gm.GetUserGroupRoles(""); //Returns Group Id and Role Id by User Id var 
groupRoles = gm.GetGroupRoles(""); //Returns Group Roles by using Group Id
string[] groupRoleNames = groupRoles.Select(p => p.Name).ToArray(); //Assing Group Role Names to a string array


//Returns Group Id and Role Id by using User Id parameter
var userGroupRoles = groupManager.GetUserGroupRoles(""); 
foreach (var role in userGroupRoles)
{
    string roleName = RoleManager.FindById(role.ApplicationRoleId).Name;
    UserManager.AddToRole(user.Id, roleName);
}


//Sets the uıser's group id
var defaultGroup = "";
groupManager.SetUserGroups(newUser.Id, new string[] { defaultGroup });

通过使用它们,现在我可以轻松地检索我需要的必要值。非常感谢@rashfmnb。