[英]asp.net c# membership: how to do a GetUsersInRoles (multiple roles)H
I want do a "GetUsersInRoles", ie I want to find all MembershipUser's having at least one of the roles in a set of roles, but I can't seem to get my head around this one. 我想做一个“GetUsersInRoles”,即我想找到所有MembershipUser在一组角色中至少有一个角色,但我似乎无法理解这个角色。
I've got GetUsersInRole, Membership.GetAllUsers(), Linq,... but how? 我有GetUsersInRole,Membership.GetAllUsers(),Linq,......但是怎么样?
Any feedback is greatly appreciated 任何反馈都非常感谢
Tommy 汤米
Here's a Linq version, it returns a MembershipUserCollection
just like the similar Membership methods ( FindUsersByEmail
, FindUsersByName
). 这是一个Linq版本,它返回一个MembershipUserCollection
,就像类似的Membership方法( FindUsersByEmail
, FindUsersByName
)一样。 It's not very pretty since it relies on the ForEach side effect: 它不是很漂亮,因为它依赖于ForEach的副作用:
public static MembershipUserCollection FindUsersByRole(string[] roles)
{
MembershipUserCollection msc = new MembershipUserCollection();
roles.Select(role => Roles.GetUsersInRole(role))
.Aggregate((a, b) => a.Union(b).ToArray())
.Distinct()
.Select( user => Membership.GetUser(user))
.ToList().ForEach( user => msc.Add(user));
return msc;
}
Alternatively if a list of MembershipUser will do: 或者,如果MembershipUser列表将执行:
public static List<MembershipUser> FindUsersByRole(string[] roles)
{
var userList = roles.Select(role => Roles.GetUsersInRole(role))
.Aggregate((a, b) => a.Union(b).ToArray())
.Distinct()
.Select( user => Membership.GetUser(user))
.ToList();
return userList;
}
And finally if you just need the user names you can skip one select: 最后,如果您只需要用户名,则可以跳过一个选择:
public static List<string> FindUsersByRole(string[] roles)
{
var userList = roles.Select(role => Roles.GetUsersInRole(role))
.Aggregate((a, b) => a.Union(b).ToArray())
.Distinct()
.ToList();
return userList;
}
There is not such method as GetUsersInRoles but you will have to use GetUsersInRole and loop through your required roles. GetUsersInRoles没有这样的方法,但你必须使用GetUsersInRole并循环遍历所需的角色。
Bit complicated but here is one way: 有点复杂,但这是一种方式:
string[] roles = {"role1", "role2" };
string[] tempusers = new string[]{};
List<string> users = new List<string>();
foreach (string role in roles)
{
string[] usersInRole = Roles.GetUsersInRole(role);
users = tempusers.Union(usersInRole).ToList();
tempusers = users.ToArray();
}
foreach (string user in users) { Response.Write(user + "<br/>"); }
Really ASP.Net membership provider seems to be built with the thought of a user being tied to a single role. 实际上ASP.Net成员资格提供者似乎是建立在用户被绑定到单个角色的想法之上的。 however we've had similar setups on projects we've worked on. 但是我们已经对我们所研究的项目进行了类似的设置。 its a little awkward but try something like this which worked for us: 它有点尴尬,但尝试这样的东西,对我们有用:
List<string> roleset_to_find = new List<string>() {"RoleA","RoleB"};
List<string> membersFound = new List<string>();
foreach (string role in roleset_to_find)
{
membersFound.AddRange(Roles.GetUsersInRole(role));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.