2

I have a new asp.net (core 1.0) mvc6 web application with the latest version of identity 3.x.

I am trying to figure out what I have to do on my own and what is already plug and play for me with Identity 3.x.

Here is my goal:

I want to create the following:
UserManagement page (will list the users from AspNetUsers table and can add new users here)

UserDetail page (can add roles to a user here which will get saved in the AspNetUserRoles table)

RoleManagement page (will list the roles from AspNetRoles table and can add new roles to the system here)

I am trying to figure out: If how much of the above the built in UserManager class can help me out with. I noticed there are RemoveFromRoleAsync, RemoveFromRolesAsync, AddToRoleAsync, and AddToRolesAsync functions. I can most likely call these functions to do the saving, but I am assuming I will have to create my own functions to list the Users and Roles in grids.

Blake Rivell
  • 13,105
  • 31
  • 115
  • 231
  • Just so everyone knows it ended up being really easy for me to use these managers to create full User, Role, and UserRole management in my application. There is a function for every single thing I needed. I just had to create the Action Methods in my Controller for all of the CRUD. – Blake Rivell Apr 17 '16 at 15:16

1 Answers1

2

You can work with the UserManager and the RoleManager. UserManager has Users and RoleManager has Roles properties.

//assuming you have injected these into your controller's constructor
var users = _userManager.Users.ToList();
var roles = _roleManager.Roles.ToList();

If you want to learn more of what you can do with Identity, we did a free course on identity https://mva.microsoft.com/en-us/training-courses/customizing-asp-net-authentication-with-identity-8647

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
  • I ended up creating two controllers: Roles Controller and Users Controller using the built-in async scaffolding. Then like you said I injected the managers as necessary and made all ActionMethods work! One weird thing is that it isn't possible to make the Index Action method which simply returns _userManager.Users.ToList(); Async... Do you know of a way? I can't put await before it since .Users isn't an async method. – Blake Rivell Apr 16 '16 at 08:45
  • Disregard my previous question, I ended up importing Microsoft.Data.Entity and just calling .ToListAsync(); at the end of _roleManager.Roles.Select – Blake Rivell Apr 16 '16 at 08:52