在ASP.NET中的IdentityUserRole 2.0中获取角色名称

Mar*_*BVI 12 c# asp.net-mvc asp.net-roles asp.net-identity

在更新实体框架中的dll之前,我能够做到这一点

user.Roles.Where(r => r.Role.Name == "Admin").FisrtOrDefault(); 
Run Code Online (Sandbox Code Playgroud)

现在,我只能做r.RoleId,我无法找到一种方法来检索thar Role Id的名称.我在我的控制器和AuthorizeAttribute类中使用它.

有人可以帮我吗?

问候

Eri*_*ips 8

询问RoleMananger

RoleManager.Roles.
// or
RoleManager.FindByIdAsync()
// or 
RoleManager.FindByNameAsync()
Run Code Online (Sandbox Code Playgroud)

您可能需要花些时间学习Asp.Net SecurityAsp.Net Identity中的新安全功能.


小智 8

试试这个

string id = UserManager.FindByEmail(model.Email).Id;
IList<string> roleNames=UserManager.GetRoles(id);
Run Code Online (Sandbox Code Playgroud)