I use the following code to search Global Catalog:
public SearchResultCollection SearchGlobalCatalog(string username)
{
var de = new DirectoryEntry("GC://SERVERNAME", "USERNAME", "PASSWORD");
var ds = new DirectorySearcher(de);
ds.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=" + username + "*))";
ds.SearchScope = SearchScope.Subtree;
var searchResults = ds.FindAll();
return searchResults;
}
Now, the problem is I don't know how to get a list of UserPrincipal
objects from SearchResultCollection
. The reason I want to do that is to get access to some user properties that are not available in Global Catalog, like Employee ID.
A
UserPrincipal
is a part of theSystem.DirectoryServices.AccountManagement
namespace.Use the helper classes in that namespace to get
UserPrincipal
objects.Without using a
UserPrincipal
try something like this: