How to list all users in Active Directory in DropD

2020-07-10 08:56发布

问题:

I am using Visual Studio 2005 C#.

I am trying to retrieve the list of users in my Active Directory and insert them into a DropDownList control.

May I know how do I pull out the users and how can I insert them into the DropDownList control?


EDIT:

There are many parts of the functionality I would wish to complete.

Firstly is to list all users in a DropDownList, and having 2 checkboxes, User and Admin, and base on the role assigned to the user in the DDL, appropriate checkboxes will be checked.

Checking and unchecking of the role checkboxes will also assign/revoke the roles accordingly.

回答1:

If you're on .NET 3.5 and up, you can use a PrincipalSearcher and a "query-by-example" principal to do your searching:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for all users
UserPrincipal qbeUser = new UserPrincipal(ctx);

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
}

If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement

This code could be rather slow - especially if you have a large AD, and a large number of users in your AD. But then again: is it really helpful to list thousands of users in a single drop down?? You might need to rethink your strategy there....

Update: if you cannot use .NET 3.5, you'll have to use the "legacy" DirectorySearcher class instead - something like this:

// find your default naming context
DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE");
string defaultCtx = deRoot.Properties["defaultNamingContext"].Value.ToString();

// define a directory searcher for your default context
string searchRootLDAPPath = "LDAP://" + defaultCtx;
DirectoryEntry defaultDE = new DirectoryEntry(searchRootLDAPPath);

// define searcher - search through entire subtree, search for users 
// (objectCategory=Person)           
DirectorySearcher dsAllUsers = new DirectorySearcher(defaultDE);
dsAllUsers.SearchScope = SearchScope.Subtree;
dsAllUsers.Filter = "(objectCategory=Person)";

// get the results
SearchResultCollection result = dsAllUsers.FindAll();

// count the user objects found
int count = result.Count;

As I mentioned - this will not perform very well on a large AD, and you might run into limits (like max. of 1'000 users returned) and other issues. You should maybe allow users to search for users, e.g. by their name or something - rather than listing out all your users (depending on the size of your AD).



回答2:

Like marc_s answer, also my answer depends on .Net 3.5. But only due to the fact that it uses LINQ, which can be translated back into some foreach-loops, make the code also runnable under .Net 2.0.

But due to the fact that i'm a fan of DRY here is simply the link to an old answer of myself.



回答3:

For current Domain ,

DirectorySearcher AllUsers = new DirectorySearcher(Domain.GetCurrentDomain().GetDirectoryEntry());
AllUsers.SearchScope = SearchScope.Subtree;
AllUsers.Filter = "(&(objectClass=user)(objectCategory=person))";
int count = AllUsers.FindAll().Count;