You can use PrincipalSearcher and query-by-example to search:
// create your domain context PrincipalContext ctx = new PrincipalContext(ContextType.Domain); // define a "query-by-example" principal - here, we search for a UserPrincipal // and with the first name (GivenName) of "Bruce" UserPrincipal qbeUser = new UserPrincipal(ctx); qbeUser.GivenName = "Bruce"; // 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 done so, absolutely read the MSDN article, "Security Principles in the .NET Framework 3.5," which shows how to make better use of the new features in System.DirectoryServices.AccountManagement
Update:
Of course, depending on your needs, you can specify other properties in this user-created request-by-example principle:
Surname (or last name)DisplayName (usually: first name + space + last name)SAM Account Name - Windows / AD account nameUser Principal Name - your username@yourcompany.com username
You can point any of the properties to UserPrincipal and use them as an “example request” for your PrincipalSearcher .
marc_s
source share