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

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

问题描述

使用 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天全站免登陆