G. M. Nazmul Hossain
G. M. Nazmul Hossain

Reputation: 466

How to get users based on role?

How could users in a "customer" role be retrieved from a MembershipUserCollection?

Upvotes: 6

Views: 4380

Answers (4)

Masoud Abyar
Masoud Abyar

Reputation: 21

For binding to a ListBox you can use:

ListBox1.DataSource = System.Web.Security.Roles.GetUsersInRole("Role_Name");
ListBox1.DataBind();

Upvotes: 1

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421968

Roles.GetUsersInRole returns a string[] of user names in a role. If you really want the MembershipUser objects, you can use:

var list = Roles.GetUsersInRole("roleName").Select(Membership.GetUser).ToList()

Of course, this is performance intensive as it hits the database once for every user.

If you are willing to give up provider-independence, you can query the underlying database directly and perform a join on the database server to get all users in a specific role.

Upvotes: 13

Rob
Rob

Reputation: 10248

Use the RoleProvider class http://msdn.microsoft.com/en-us/library/system.web.security.roleprovider.aspx

it has a FindUsersInRole method

Upvotes: 0

Related Questions