Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All User with Associated Roles #258

Closed
sajidali2444 opened this issue Aug 18, 2019 · 1 comment
Closed

All User with Associated Roles #258

sajidali2444 opened this issue Aug 18, 2019 · 1 comment

Comments

@sajidali2444
Copy link

@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
Copy link
Collaborator

asadsahi commented Nov 7, 2021

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

@asadsahi asadsahi closed this as completed Nov 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants