2011-01-10 62 views
0

我可以通過以下方式查詢用戶,但是我想向搜索DN添加額外的OU = Good。這可能沒有硬編碼GC的完整路徑?如何從GlobalCatalog添加OU

Forest myForest = Forest.GetCurrentForest(); 
GlobalCatalog cat = myForest.FindGlobalCatalog(); 
DirectorySearcher srch = cat.GetDirectorySearcher(); 
srch.Filter = "(&(objectClass=user)(samaccountname=myuser))"; 
SearchResult result = srch.FindOne(); 

回答

1

你可能嘗試獲得DirectoryEntry爲全局編錄,然後搜索其.Children屬性你正在尋找的OU,然後使用該DirectoryEntry作爲搜索根 - 像(未經測試,不要「T有AD手頭測試):

Forest myForest = Forest.GetCurrentForest(); 
GlobalCatalog cat = myForest.FindGlobalCatalog(); 

DirectoryEntry deGlobal = cat.GetDirectoryEntry(); 

DirectoryEntry deYourOU = deGlobal.Children.Find("OU=Good"); 

DirectorySearcher srch = new DirectorySearcher(deYourOU); 

當然,在現實中,你會想一些錯誤檢查(!= null等)添加到您的代碼 - 但是這將是基本途徑獲取你的OU(我希望!)