I want to search for users in an Active Directory environment with GC://DC=xxx,DC=yyy,DC=zzz
format. But how can I programmatically find the global catalogs in an arbitary Active Directory environment? Does each domain name correspond to a global catalog always? Any alternative means I can try?
Note: The Forest.FindAllGlobalCatalogs()
returns a list of server names but I'm actually not able to search using them.
Edit1: Here's what I want to do : Suppose my activedirectory has a domain called domain1.root.com, then I will use GC://DC=domain1,DC=root,DC=com to search for a user. But is this always a Global catalog? Must every domain have a global catalog?
Edit2: I am now able to search for users using the following code:
var currentForest = Forest.GetCurrentForest();
var globalCatalog = currentForest.FindGlobalCatalog();
Console.WriteLine(globalCatalog.Name);
//DirectorySearcher searcher = new DirectorySearcher("GC://"+y.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();