Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get user with assigned roles

Using ASP.NET Core identity, I create a new user with UserManager.CreateAsync() and assign them to an existing role with UserManager.AddToRoleAsync. This works, as the realtion between user and role is stored in the AspNetUserRoles table of the database.

But when I fetch the user using the UserManager (e.g. UserManager.FindByMail() method) then the Role list is empty. I also tried the Include function from EF like this:

var user = userManager.Users.Include(u => u.Roles).FirstOrDefault(u => u.Email == "[email protected]");

This gave me the Ids of the n:m association table, which is not very usefull as I need the role names. Loading them using a second query is also not possible, since the Roles attribute of the identity user is readonly. I would expect to get a List<string> of the role-names. Couldn't find any information about this.

For me the only workaround seems to add a custom attribute to my user and fill them with the data, which I fetch using a second query. But thats not a nice solution. Cant belive that ASP.NET Core Identity has no better way of getting those data...

like image 649
Lion Avatar asked Oct 09 '16 17:10

Lion


People also ask

Which Microsoft Azure PowerShell command should you run to assign a user to a role?

In a PowerShell window, use Connect-AzureAD to sign in to your tenant. Use Get-AzureADUser to get the user you want to assign a role to.


1 Answers

UserManager does not load the relations by default.

The manual inclusion is a good way but as stated here - direct M:N relationships are not yet supported by EntityFramework Core.

So there are two ways I see:

(The preffered one) Use

    userManager.GetRolesAsync(user);

This will return a List<string> with user's role names. Or use some EF query to get an IdentityRole objects by joined IdentityUserRole. Unfortunatelly, this requires an acceptance with the fact the roles will not be directly in the User entity.

OR you can implement custom IdentityUserRole, create a relation to IdentityRole there and then query it with `

Include(user => user.Roles).ThenInclude(role => role.Role)

How to implement own Identity entities is described e.g. here. But it's complicated approach and the Role objects will be nested in the binding entities.


However, you can declare a property in your ApplicationUser:

[NotMapped]
public List<string> RoleNames {get; set;}

and use it at you free will...

like image 104
rudolfdobias Avatar answered Oct 24 '22 14:10

rudolfdobias