BOT Elmo
BOT Elmo

Reputation: 69

Blazor Displaying roles from users ASP.NET

Here I got my GetUsers

public List<IdentityUser> GetUsers()
    {
        SeeUsers = new List<IdentityUser>();
        var user = usermanager.Users.Select(x => new IdentityUser
        {
            Id = x.Id,
            UserName = x.UserName,
            Email = x.Email
        });
        foreach (var item in user)
        {
            SeeUsers.Add(item);
        }
        return user.ToList();
    }

This is my page

    <AuthorizeView Roles="Admin" Context="Beheer">
        <table class="table">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>User Name</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var user in users)
                {
                    <tr>
                        <td>@user.UserName</td>
                        <td>@user.Email</td>
                    </tr>
                }
            </tbody>
        </table>
    </AuthorizeView>
</AuthorizeView>
@code {
    List<IdentityUser> users = new List<IdentityUser>();
    List<IdentityRole> roles = new List<IdentityRole>();
    protected override void OnInitialized()
    {
        users = UserService.GetUsers();
    }
}


the problem is I need to add the user role from aspnetuserroles to a table and I cant figgure out how to do it.
I tried rolemanager but that would only give the name and id of a role and not the bound user to it.
Is there a way to get the info straight out of aspnetuserroles?

I want something like this
Preview

Upvotes: 1

Views: 2046

Answers (1)

Nik FP
Nik FP

Reputation: 3063

If you are using ApplicationDbContext, you should have access to the ASPNetRoles and ASPNetUserRoles tables through the EF Core API. You can skip the UserManager and RoleManager and build out a service (or add to your UserService) that gets the info you need direct from the Database. Once you have Users, Roles, and UserRoles you can construct view logic to display it all as needed.

Upvotes: 1

Related Questions