Getting All Users and All Roles through asp.net Identity

后端 未结 4 1000
栀梦
栀梦 2020-12-18 21:34

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 applicati

相关标签:
4条回答
  • 2020-12-18 21:54

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

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

    0 讨论(0)
  • 2020-12-18 22:15

    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.

    0 讨论(0)
  • 2020-12-18 22:17

    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();
    }
    
    0 讨论(0)
  • 2020-12-18 22:21

    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.

    0 讨论(0)
提交回复
热议问题