如何检索用户的ASPNET身份通过LINQ的特定角色 [英] How to retrieve Users with a specific role via Linq in ASPnet Identity
问题描述
我需要检索的,这是我认为应该进行直接的一个特定角色的所有用户,但是,这里的问题。
I need to retrieve all users of a specific role, something I think should be made straightforward, however, here is the problem.
ApplicationUser的作用导航属性不是角色列表(IdentityRole),但(IdentityUserroles)集合链接用户和角色的中间表。所以,这是code我有,但它不能正常工作。
The Roles navigation Property of ApplicationUser is not the list of roles (IdentityRole) but a collection of (IdentityUserroles) the intermediate table that links users and roles. So, this is the code I have, but it does not work.
[HttpPost]
[Authorize(Roles = "Admin, Recepcionista, Orientador")]
public ActionResult SearchResults(SearchCriteriaViewModel criteria)
{
List<SearchResultViewModel> results = new List<SearchResultViewModel>();
var clients = new List<ApplicationUser>();
using (var context = new ApplicationDbContext())
{
IdentityUserRole role = new IdentityUserRole();
var tempRole = (from _role in context.Roles
where _role.Name == "client"
select _role).FirstOrDefault();
role.Role = tempRole;
clients = (from client in context.Users
where client.Email.Contains(criteria.SearchCriteria)
|| client.FirstName.Contains(criteria.SearchCriteria)
|| client.MiddleName.Contains(criteria.SearchCriteria)
|| client.LastName.Contains(criteria.SearchCriteria)
|| client.SecondLastName.Contains(criteria.SearchCriteria)
|| client.UserName.Contains(criteria.SearchCriteria)
&& client.Roles.Contains(role)
select client).ToList();
}
foreach (var client in clients)
{
results.Add(new SearchResultViewModel()
{
Email = client.Email,
FirstName = client.FirstName,
MiddleName = client.MiddleName,
LastName = client.LastName,
SecondLastName = client.SecondLastName,
UserName = client.UserName
});
}
SearchClientViewModel model = new SearchClientViewModel();
model.SearchResults = results;
return View("Index", model);
}
Linq的抛出以下异常
Linq throws the following Exception
型的System.NotSupportedException异常发生在
mscorlib.dll中,但在用户code没有处理
An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code
信息:无法创建类型的常数值
Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole。只要
基本类型或枚举类型,在这方面的支持。
Additional information: Unable to create a constant value of type 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole'. Only primitive types or enumeration types are supported in this context.
如何才能做到这一点,为什么导航性能是不是角色的集合,这会是我们需要报告的错误?
How can this be done, Why the navigation properties is not a collection of Roles, could this be a bug we need to report?
推荐答案
错误信息很清楚:你不能叫包含
对非原始对象的集合。
Error message is quite clear: you can't call Contains
on collection of non-primitive objects.
尝试以下操作:
clients = (from client in context.Users
where client.Email.Contains(criteria.SearchCriteria)
|| client.FirstName.Contains(criteria.SearchCriteria)
|| client.MiddleName.Contains(criteria.SearchCriteria)
|| client.LastName.Contains(criteria.SearchCriteria)
|| client.SecondLastName.Contains(criteria.SearchCriteria)
|| client.UserName.Contains(criteria.SearchCriteria)
&& client.Roles.Select(r => r.Name).Contains("client")
select client).ToList();
这篇关于如何检索用户的ASPNET身份通过LINQ的特定角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!