Getting All Users and All Roles through asp.net Identity

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?

Answers


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.


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

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.


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

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


Need Your Help

:hover:before text-decoration none has no effects?

css css-selectors pseudo-element

As title, I'm adding icons using .icon-*. When adding an icon to an hyperlink:

How do I wait until Task is finished in C#?

c# .net task task-parallel-library wait

I want to send a request to a server and process the returned value: