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.