ASP.Net MVC List of User and their Roles

user8270367

I'm using ASP.NET MVC Default project. In there we have a database called AspNetRoles (defines roles like Admin, User)
Role Database

then there's a database called AspNetUsers which just defines Users when you register them User Database

and the last and most important is AspNetUserRoles. When you assign Roles to the User here is where it is placed (and the Id's are hashed)

UserRole Database

My question is, how do I get a list of every user on the system and their roles? I can't figure out how to start doing this. Should I create my own User and Role database and forget about the default one? Or is there a way of creating with the default ones?


Also, I would keep in mind that as of now I'm assigning Roles when registering (User by default) and later when I create the Index where I show the list of Users and Roles, I will want to edit them, but this is just a disclaimer. Thank you very much for any type of help.

Valkyrie

There are no built in methods for retrieving the list of users with their roles so you can try to get the user and their role like below (using EF):

var list = context.Users        
    .Where(x => x.Roles.Select(a => a.Id))
    .ToList();

Using this you get every users and assigned roles.

Update Create a Model like below first:

public class UserRoleInfo
{
    public ApplicationUser User { set; get; }
    public List<string> Roles { set; get; } 
}

Then Your Query would be like (Query Syntax):

var listOfUsers = (from u in db.Users
             let query = (from ur in db.Set<IdentityUserRole>()
                          where ur.UserId.Equals(u.Id)
                          join r in db.Roles on ur.RoleId equals r.Id select r.Name)
                          select new UserRoleInfo() {User = u, Roles = query.ToList<string>()})
                         .ToList();

Query above will return Every User With their Roles no matter how many.

UPDATE

Your view would be like:

@model UserRoleInfo
    <table>
        <tbody>
@foreach (var user in Model)
{
    var appUser = user.ApplicationUser;
    var roles = user.Roles;

            <tr>
                <td>
                    @appUser.Email
                </td>
                <td>
                    @string.Join(",", roles)
                </td>
            </tr>

}
        </tbody>
    </table>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Creating Roles in Asp.net Identity MVC 5

ASP.NET MVC Authorize user with many roles

Custom user authorization based with roles in asp.net mvc

List of Users with roles in MVC Asp.net identity

How to add a simple user roles - ASP.NET MVC C#

user roles and permission in mvc

How to turn on Roles in Asp.net Identity 3.0 and MVC 6?

Adding custom roles to windows roles in ASP.NET MVC 5

Get all users and their roles they got into a table asp.net mvc

ASP.Net MVC Authentication - Hide Element in View based on roles

Discord.net get the list of roles for a user

How to structure a .NET Core MVC project for multiple user roles?

How to use autentication with roles on ASP.NET MVC

ASP.Net MVC SimpleMembershipProvider and using Roles

ASP.net 4.5 Membership and Roles add user to role

Individual page authorization based on roles in ASP.NET MVC 5

How to check user is in many roles in asp.net identity

ASP.net Identity OWIN cookie when user roles change?

Custom Role Provider with ASP.net MVC -- Changing Roles

Adding roles in the create user view ASP Identity MVC

ASP.NET (MVC) Users, Roles and Users in Roles

ASP.NET MVC Blacklist for Roles/Users

Load list of roles for each user in MVC5 razor

Asp.net Mvc display list depending on User

Get User Roles with ASP.net Identity and Web API

ASP .Net Core MVC RoleManager Roles To List Retun Object reference not set to an instance of an object

ASP.NET MVC Displaying a user's roles as comma separated string

Using claims or just roles in ASP.NET Core MVC

Asp.net Core MVC Roles and Authorization