fullstackproltd / AspNetCoreSpa

Asp.Net 7.0 & Angular 15 SPA Fullstack application with plenty of examples. Live demo:
https://aspnetcorespa.fullstackpro.co.uk
MIT License
1.48k stars 465 forks source link

All User with Associated Roles #258

Closed sajidali2444 closed 3 years ago

sajidali2444 commented 5 years ago

@asadsahi Hi, A quick question may be helpful to others. Is it possible with your current code base we can get all users with associated roles? If you provide me a code sample or help really thankful to you. when i try to join user with roles there is no roles property and if i add this property then its not return roles. here is my code can you help me . var users = _userManager.Users.Include(u => u.UserRoles).ThenInclude(ur => ur.Role).ToList();

public class ApplicationUser : IdentityUser { public int UserId { get; set; } public ICollection UserRoles { get; set; } }

public class ApplicationRole : IdentityRole { [StringLength(100)] public string Description { get; set; } public ICollection UserRoles { get; set; } }

public class ApplicationUserRole : IdentityUserRole { //public int Id{get;set;} public virtual ApplicationUser User { get; set; } public virtual ApplicationRole Role { get; set; } }

asadsahi commented 3 years ago

I think you are at the right path, you need to have have access to all identity schema and then include UserRole and Role, like in your example and then project that to your custom view model to include all roles. This link explains how to access all this schema:

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-6.0#add-all-user-navigation-properties