简体   繁体   English

让所有用户都扮演角色时出错

[英]error with getting all users in role

I'm trying to get a list of all users in a role, and despite the numerous questions and links I've read on here, I can't seem to figure this Identity stuff out. 我试图获取一个角色中所有用户的列表,尽管我在这里阅读了许多问题和链接,但似乎无法弄清楚这个Identity的内容。 Below is what I'm currently working with: 以下是我目前正在使用的工具:

IdentityRole role = context.Roles.Where(x => x.Name == rolesDDL.SelectedValue).First();
var users = context.Users.Where(x => x.Roles.Any(r => r.RoleId == role.Id));

However this gets an error that says "Sequence contains no elements" on the role line. 但是,这会得到一个错误,在角色行上显示“序列不包含任何元素”。 I tried doing a similar bit of code with the UserManager/RoleManager but that had the same result: 我尝试使用UserManager / RoleManager做类似的代码,但是结果相同:

var userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
var roleManager = Request.GetOwinContext().GetUserManager<ApplicationRoleManager>();

IdentityRole role = roleManager.Roles.Where(x => x.Name == rolesDDL.SelectedValue).First();
var users = userManager.Users.Where(x => x.Roles.Any(r => r.RoleId == role.Id));

EDIT: It seems to be an issue with the RoleManager not being able to actually find my roles. 编辑:RoleManager似乎无法真正找到我的角色,这似乎是一个问题。 If I go into ServerExplorer I can see the roles in my AspNetRoles table but after instantiating my RoleManager from the OWIN context it doesn't seem to have any roles still. 如果我进入ServerExplorer,我可以在AspNetRoles表中看到角色,但是从OWIN上下文实例化RoleManager之后,它似乎还没有任何角色。 Could this be an issue from installing RavenDB for the data? 为数据安装RavenDB可能会引起问题吗? I saw how to migrate users to Raven also but I was hoping to get it working with defaults first, before I made more issues for myself. 我也看到了如何将用户迁移到Raven,但我希望在我自己解决更多问题之前,先将其与默认值一起使用。

This is what I currently have in this function: 这是我目前在此功能中拥有的:

var userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
var roleManager = Request.GetOwinContext().GetUserManager<ApplicationRoleManager>();

//IdentityRole role = roleManager.Roles.Where(x => x.Name == rolesDDL.SelectedValue).First();
//var users = userManager.Users.Where(x => x.Roles.Any(r => r.RoleId == role. 

//IdentityRole role = context.Roles.Where(x => x.Name == rolesDDL.SelectedValue).First();
//var users = context.Users.Where(x => x.Roles.Any(r => r.RoleId == role.Id));

var users = roleManager.FindByName(rolesDDL.SelectedItem.Text).Users;

EDIT: Adding initializers per request, found in startup.auth.cs: 编辑:在startup.auth.cs中找到每个请求添加初始化程序:

app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

If you use your roleManager instead and filter it with the role name rather than the role id you can use the following: 如果改为使用roleManager并使用角色名称而不是角色ID对其进行过滤,则可以使用以下内容:

var roleManager = HttpContext.Current
        .GetOwinContext()
        .Get<ApplicationRoleManager>();

var roleName = rolesDDL.SelectedItem.Text;
var users = roleManager.FindByName(roleName).Users

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM