asp.net c# membership: how to do a GetUsersInRoles (multiple roles)H
Asked Answered
V

3

7

I want do a "GetUsersInRoles", i.e. 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.

I've got GetUsersInRole, Membership.GetAllUsers(), Linq,... but how?

Any feedback is greatly appreciated

Tommy

Vitriolic answered 2/1, 2011 at 20:56 Comment(1)
This would have taken about 10 seconds to write in a stored proc.Felecia
D
10

Here's a Linq version, it returns a MembershipUserCollection just like the similar Membership methods (FindUsersByEmail, FindUsersByName). It's not very pretty since it relies on the ForEach side effect:

    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:

    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;
    }
Drooff answered 2/1, 2011 at 23:13 Comment(1)
Thanks a lot for all your responses and quick answers. Unfortunately I cannot add "usefulness"-points as I lack reputation points myself.Vitriolic
S
1

There is not such method as GetUsersInRoles but you will have to use GetUsersInRole and loop through your required roles.

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/>"); }
Snips answered 2/1, 2011 at 21:7 Comment(1)
Right - that's what I'm looking for a nice way of doing, preferably using LinqVitriolic
A
1

Really ASP.Net membership provider seems to be built with the thought of a user being tied to a single role. 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));
 }
Anemia answered 2/1, 2011 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.