通过 asp.net Identity 获取所有用户和所有角色 [英] Getting All Users and All Roles through asp.net Identity
问题描述
我有一个在 VS 2013 中创建的新项目.我正在使用身份系统,但我很困惑如何获取应用程序的所有用户和应用程序中的所有角色的列表.我正在尝试创建一些管理页面,以便我可以添加新角色、向用户添加角色、查看谁都已登录或已锁定.
I have a new project i created in VS 2013. I am using the identity system and i'm confused how to get a list of all users to the application and all roles int he application. I am trying to create some admin pages so i can add new roles, add roles to users, see who all is logged in or locked.
有人知道怎么做吗?
推荐答案
在 ASP.NET Identity 1.0 中,您必须从 DbContext 本身获取此信息...
In ASP.NET Identity 1.0, you'll have to get this from the DbContext itself...
var context = new ApplicationDbContext();
var allUsers = context.Users.ToList();
var allRoles = context.Roles.ToList();
在 ASP.NET Identity 2.0(目前处于 Alpha 版)中,此功能公开在 UserManager
和 RoleManager
...
In ASP.NET Identity 2.0 (currently in Alpha), this functionality is exposed on the UserManager
and RoleManager
...
userManager.Users.ToList();
roleManager.Roles.ToList();
在这两个版本中,您将与 RoleManager
和 UserManager
交互以创建角色并将角色分配给用户.
In both versions, you would be interacting with the RoleManager
and UserManager
to create roles and assign roles to users.
这篇关于通过 asp.net Identity 获取所有用户和所有角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!