获取分配了角色的用户 [英] Get user with assigned roles

查看:52
本文介绍了获取分配了角色的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ASP.NET Core身份,使用UserManager.CreateAsync()创建一个新用户,然后使用UserManager.AddToRoleAsync将他们分配给现有角色.之所以可行,是因为用户和角色之间的关系存储在数据库的AspNetUserRoles表中.

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.

但是当我使用UserManager(例如UserManager.FindByMail()方法)获取用户时,Role列表为空.我也像这样尝试了EF的Include函数:

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 == "test@test.de");

这给了我n:m关联表的ID,因为我需要角色名称,所以它不是很有用.由于身份用户的Roles属性是只读的,因此也无法使用第二个查询来加载它们.我希望得到角色名称的List<string>.找不到有关此的任何信息.

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.

对我来说,唯一的解决方法似乎是向用户添加自定义属性,并用数据填充它们,我使用第二个查询来获取数据.但这不是一个很好的解决方案.不能相信ASP.NET Core Identity没有更好的方法来获取这些数据...

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...

推荐答案

默认情况下,UserManager不会加载关系.

UserManager does not load the relations by default.

手动添加是一种好方法,但是如此处-EntityFramework Core尚不支持直接的M:N关系.

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:

(首选)使用

    userManager.GetRolesAsync(user);

这将返回带有用户角色名称的List<string>.或使用某些EF查询通过联接的IdentityUserRole获取IdentityRole对象.不幸的是,这需要接受事实,即角色将不会直接位于用户实体中.

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.

或者您可以实现自定义IdentityUserRole,在此处创建与IdentityRole的关系,然后使用`

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)

例如,描述了如何实现自己的身份实体. 此处.但这是复杂的方法,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.

但是,您可以在ApplicationUser中声明一个属性:

However, you can declare a property in your ApplicationUser:

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

并免费使用它...

这篇关于获取分配了角色的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆