Link to home
Start Free TrialLog in
Avatar of Peter H.
Peter H.Flag for Canada

asked on

Linq query to get a list of roles for a user in identity framework using blazor c#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;

I want to get a list with UserName, UserId, and a string with all the roles that the user is assigned to. 

Basically, I want to return  "User's Id", "UserName", "Admin, User, Guest" (roles)

What I have so far is shown below, Just don't know how to do the ????? to get the list.

_db is just public class ApplicationDbContext : IdentityDbContext<IdentityUser>

            var users = from u in _db.Users
                join rr in _db.UserRoles on u.Id equals rr.UserId                 join r in _db.Roles on rr.RoleId equals r.Id                 select new UserWithRoles { UserName = u.UserName,                     UserId = u.Id,                     AssignedRoles = String.Join(", ",??????) };

Open in new window

Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Just curious, but if you are already using identity core, why not just use UserManager.GetRolesAsync to get the roles?

https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.usermanager-1.getrolesasync?view=aspnetcore-5.0
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial