Getting All Users and All Roles through asp.net Id

2019-01-26 05:29发布

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.

Anybody know how to do this?

4条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-26 05:58

Building on what Anthony Chu said, in Identity 2.x, you can get the roles using a custom helper method:

public static IEnumerable<IdentityRole> GetAllRoles()
{
    var context = new ApplicationDbContext();
    var roleStore = new RoleStore<IdentityRole>(context);
    var roleMgr = new RoleManager<IdentityRole>(roleStore);
    return roleMgr.Roles.ToList();
}
查看更多
别忘想泡老子
3楼-- · 2019-01-26 06:00

System.Web.Security Roles class also allows obtaining the list of roles.

List<String> roles = System.Web.Security.Roles.GetAllRoles();

查看更多
你好瞎i
4楼-- · 2019-01-26 06:02

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

In ASP.NET Identity 2.0 (currently in Alpha), this functionality is exposed on the UserManager and RoleManager...

userManager.Users.ToList();
roleManager.Roles.ToList();

In both versions, you would be interacting with the RoleManager and UserManager to create roles and assign roles to users.

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-26 06:03

Building on Anthony Chu and Alex.
Creating two helper classes...

    public class UserManager : UserManager<ApplicationUser>
      {
        public UserManager() 
            : base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
            { }
       }

     public class RoleManager : RoleManager<IdentityRole>
     {
        public RoleManager()
            : base(new RoleStore<IdentityRole>(new ApplicationDbContext()))
        { }
      }

Two methods to get the roles and users.

   public static IEnumerable<IdentityRole> GetAllRoles()
    {
        RoleManager roleMgr = new RoleManager();
        return roleMgr.Roles.ToList();
    }

    public static IEnumerable<IdentityUser> GetAllUsers()
    {
        UserManager userMgr = new UserManager();
        return userMgr.Users.ToList();
    }

Two examples of Methods using GetRoles() and GetUsers() to populat a dropdown.

public static void FillRoleDropDownList(DropDownList ddlParm)
{
    IEnumerable<IdentityRole> IERole = GetAllRoles();

    foreach (IdentityRole irRole in IERole)
    {
        ListItem liItem = new ListItem(irRole.Name, irRole.Id);
        ddlParm.Items.Add(liItem);
    }
}

public static void FillUserDropDownList(DropDownList ddlParm)
{ 
    IEnumerable<IdentityUser> IEUser = GetAllUsers();

    foreach (IdentityUser irUser in IEUser)
    {
        ListItem liItem = new ListItem(irUser.UserName, irUser.Id);
        ddlParm.Items.Add(liItem);
    }
}

usage example:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FillRoleDropDownList(ddlRoles);
            FillUserDropDownList(ddlUser);
        }
    }

thx to Anthony and Alex for helping me understand these Identity classes.

查看更多
登录 后发表回答