16

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?

Andy Xufuris
  • 698
  • 3
  • 9
  • 31

4 Answers4

27

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.

Anthony Chu
  • 37,170
  • 10
  • 81
  • 71
  • 2
    Thank you man, this works great. Can't wait until 2.0 so it's a little easier. – Andy Xufuris Feb 04 '14 at 00:08
  • Is there a way to filter users and get those who are logged in? I can't seem to find such a field on the IdentityUser class – SoManyGoblins Apr 10 '14 at 17:42
  • 4
    This functionality doesn't exist out of the box. It's likely because it's something that is not easy to do and the definition of "logged in" is different depending on the application. For example, is a user logged in until they log out? What if they are using multiple browsers or devices and they've logged out of one of them? Perhaps "logged in" in some scenario means a user has been active within the past x minutes. How would you like it to work in your application? Perhaps that's a good starting point for a discussion. – Anthony Chu Apr 10 '14 at 20:14
4

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();
}
Alex
  • 34,699
  • 13
  • 75
  • 158
1

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.

Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
-1

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

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

Mike Dimmick
  • 9,662
  • 2
  • 23
  • 48
moarra
  • 565
  • 1
  • 7
  • 20