【发布时间】: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);
}
}
但是,由于角色是通过 ApplicationGroupRoles 和 ApplicationUserGroups 表分配给用户的,因此此 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