Search code examples
c#asp.netasp.net-identityasp.net-core-1.0asp.net-identity-3

Setting up UserRole management with MVC6 and the new Identity


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.


Solution

  • 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