I want to search for users in an Active Directory environment with a format GC://DC=xxx,DC=yyy,DC=zzz. But how can I programmatically find global directories in an arbitrary Active Directory environment? Does the domain name always match the global catalog? Any alternative ways I can try?
Note. Forest.FindAllGlobalCatalogs()returns a list of server names, but in fact I cannot use them.
Edit1: Here is what I want to do: suppose my activated directory has a domain called domain1.root.com, then I will use GC: // DC = domain1, DC = root, DC = com to search for the user. But is it always a global catalog? Should each domain have a global catalog?
Edit2: now I can search for users using the following code:
var currentForest = Forest.GetCurrentForest();
var globalCatalog = currentForest.FindGlobalCatalog();
Console.WriteLine(globalCatalog.Name);
DirectorySearcher searcher = globalCatalog.GetDirectorySearcher();
searcher.Filter = @"samaccountname=skaranth";
Console.WriteLine(searcher.SearchRoot.Path);
var result = searcher.FindOne();
if(result!=null)
Console.WriteLine(result.Properties["distinguishedname"][0]);
searcher.Dispose();
globalCatalog.Dispose();
currentForest.Dispose();
source
share