Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 492
Andy Xufuris Avatar asked Feb 02 '14 00:02

Andy Xufuris


People also ask

How do you get user role in identity?

it gives you the AspNetUserInRoles which stores UserId and RoleId. Instead you could try UserManger 's GetRoles method which will return you List<string> of roles user is assigned. But as you mentioned it will be only one role hence you can take first value from the result of GetRoles method.

How do I get user role in .NET core identity?

Show activity on this post. var user = await _userManager. FindByIdAsync(UserId); var roles = await _userManager. GetRolesAsync(user); return OK(new { User = user, Roles = roles });


1 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.

like image 129
Anthony Chu Avatar answered Nov 13 '22 17:11

Anthony Chu