List Of Users With Roles In ASP.NET MVC Identity

In this article, we will learn how to list all users with Associated Roles in ASP.NET MVC 5 using Identity. ASP.NET MVC 5 does not come with an inbuilt feature to list users with associated roles by default. However ASP.NET MVC have inbuilt UserManager, SignManager and RoleManager to assist this.
 
We need this feature in each of our applications as users are to be maintained along with their associated roles. We can apply a number of ideas to do this. In this article, we will learn a very simple way to list users with their associated RoleName as in the figure below.
 
Step 1

Create a View Model as Users-in-Role_ViewModel
 
 
Code Snippet
  1. public class Users_in_Role_ViewModel  
  2.     {  
  3.         public string UserId { getset; }  
  4.         public string Username { getset; }  
  5.         public string Email { getset; }  
  6.         public string Role { getset; }  
  7.     }  
Step 2

Add a new method called UsersWithRoles inside ManageUsersController and add the following codes.

 

Code Snippet 
  1. public ActionResult UsersWithRoles()  
  2.         {  
  3.             var usersWithRoles = (from user in context.Users  
  4.                                   select new  
  5.                                   {  
  6.                                       UserId = user.Id,                                        
  7.                                       Username = user.UserName,  
  8.                                       Email = user.Email,  
  9.                                       RoleNames = (from userRole in user.Roles  
  10.                                                    join role in context.Roles on userRole.RoleId   
  11.                                                    equals role.Id  
  12.                                                    select role.Name).ToList()  
  13.                                   }).ToList().Select(p => new Users_in_Role_ViewModel()  
  14.    
  15.                                   {  
  16.                                       UserId = p.UserId,  
  17.                                       Username = p.Username,  
  18.                                       Email = p.Email,  
  19.                                       Role = string.Join(",", p.RoleNames)  
  20.                                   });  
  21.    
  22.    
  23.             return View(usersWithRoles);  
  24.         }  
Note context.Users represents the table AspNetUsers which has navigation property roles which represent the AspNetUserInRoles table. 
 
Step 3

Now, let’s add a View of UsersWithRoles method of ManageUsersController.
  1. @model IEnumerable<MVC5Demo.UI.ViewModels.Users_in_Role_ViewModel>  
  2.    
  3. @{  
  4.     ViewBag.Title = "Users With Roles";  
  5.     Layout = "~/Views/Shared/_Layout.cshtml";  
  6. }  
  7.    
  8. <div class="panel panel-primary">  
  9.     <div class="panel-heading">  
  10.         <h3 class="box-title">  
  11.             <b>Users with Roles</b>  
  12.         </h3>  
  13.     </div>  
  14.     <!-- /.box-header -->  
  15.     <div class="panel-body">  
  16.         <table class="table table-hover table-bordered table-condensed" id="UsersWithRoles">  
  17.             <thead>  
  18.                 <tr>  
  19.                     <td><b>Username</b></td>  
  20.                     <td><b>Email</b></td>  
  21.                     <td><b>Roles</b></td>  
  22.                 </tr>  
  23.             </thead>  
  24.             @foreach (var user in Model)  
  25.             {  
  26.                 <tr>  
  27.                     <td>@user.Username</td>  
  28.                     <td>@user.Email</td>  
  29.                     <td>@user.Role</td>  
  30.                       
  31.                 </tr>  
  32.             }  
  33.         </table>  
  34.     </div>  
  35.    
  36.     <div class="panel-footer">  
  37.         <p class="box-title"><b>Total Users till @String.Format("{0 : dddd, MMMM d, yyyy}", DateTime.Now)  : </b><span class="label label-primary">@Model.Count()</span></p>  
  38.     </div>  
  39. </div>  
  40.    
  41.    
  42. @section scripts{  
  43.     <script>  
  44.         $(function () {  
  45.             $('#UsersWithRoles').DataTable({  
  46.                 "paging"true,  
  47.                 "lengthChange"true,  
  48.                 "searching"true,  
  49.                 "ordering"true,  
  50.                 "info"true,  
  51.                 "autoWidth"true  
  52.             });  
  53.         });  
  54.     </script>  
  55. }  
Result

Now, the above method and View will return the users to their roles, as shown in the following figure.



View returning list of users with their associated Roles in ASP-NET MVC.

Note

You need to have the list of users associated with each role in the database.

Source Code

https://github.com/nishanaryal/ASP.NET-MVC