【问题标题】:How to obtain roles from ASP.NET Identity ApplicationGroupRoles table如何从 ASP.NET Identity ApplicationGroupRoles 表中获取角色
【发布时间】:2016-04-05 14:40:39
【问题描述】:

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

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 表分配给用户的,因此此 UserManager.GetRoles(userId) 方法不起作用,因为它仅从AspNetUserRoles 表。那么,如何管理检索给定用户的角色,即先查看 ApplicationUserGroups 然后查看 ApplicationGroupRoles 表? 或者是使用 sql 命令检索它们的唯一方法?任何帮助,将不胜感激。

【问题讨论】:

  • 虽然我已经发布了另一种查找角色的方法,但是您的代码工作正常,您是否在表 AspNetRoles 中输入了任何角色并在表 AspNetUserRoles 中创建了用户和角色之间的关系
  • @rashfmnb 是的,我可以从 AspNetUserRoles 表中接收,但我需要获取 ApplicationGroupRoles 表的值。

标签: c# asp.net asp.net-mvc asp.net-identity asp.net-identity-2


【解决方案1】:

虽然我已经发布了另一种查找角色的方法,但是您的代码很好,您是否在表中输入了任何角色

AspNetRoles

并在表中创建用户和角色之间的关系

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;
    }

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

    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);
    }

将用户附加到角色的代码 在 cofiguration.cs

中的 protected override void Seed(RoleBasedSecurity.Models.ApplicationDbContext context) 中写入这些行
    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");
    }

【讨论】:

  • 非常感谢您的回复。刚刚抽出时间尝试测试您的答案和第一部分,并获得:au.Roles 充满了 AspNetUserRoles 表而不是 ApplicationGroupRoles。那么,正如我上面提到的,如何从 ApplicationGroupRoles 表中检索角色?提前致谢。
  • 非常感谢您的帮助。据我所知,ASP.NET Identity 2 中有一些有用的内置方法。你的回答也很有用,我很感激。谢谢和投票+
【解决方案2】:

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

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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2018-01-08
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 2014-03-08
    相关资源
    最近更新 更多