2013-04-03 98 views
0

我正在嘗試從活動目錄中獲取OU的列表。不幸的是,即使我知道「myApp」域組件中有2個OU,但我的搜索始終沒有任何結果。從Active Directory中檢索OU's

using (var entry = new DirectoryEntry("LDAP://myServer:1111/DC=myApp,DC=myDomain,DC=com", Username, Password)) { 
    using (var searcher = new DirectorySearcher()) { 
     searcher.SearchRoot = entry; 
     searcher.Filter = "(objectCategory=Organizational-Unit)"; 
     searcher.PropertiesToLoad.Add("name"); 

     //foo never gets results. :(
     var foo = searcher.FindAll(); 
    } 
} 

我試着按照先前的StackOverflow question中的代碼,但沒有運氣。

回答

0

1)確定要查找基準搜索"DC=myApp,DC=myDomain,DC=com""myApp"是域組件嗎?

2)你可以嘗試指定搜索範圍嗎?

searcher.SearchScope = SearchScope.Subtree; 

3)"(objectCategory=Organizational-Unit)"是活動目錄所理解的捷徑,但實際上objectCategory屬性是專有名稱(DN)和一個OU的真正價值是:​​。

你可以試試這個過濾器"(objectClas=Organizational-Unit)"這是更常見的搜索OU嗎?


在命令行上可以試試這個嗎?

C:\temp>ldifde -f c:\temp\out.txt -d "DC=myApp,DC=myDomain,DC=com" -r "(objectClass=organizationalUnit)" 
+0

是的,我簡單地更換值與一些虛擬文本。我直接從微軟的AD瀏覽器中取出字符串。 2.我相信'Subtree'是默認值,但是,我無論如何都沒有運氣。 3.我沒有運氣就嘗試過'(objectClass = Organizational-Unit)'。對於它的價值,如果我保持除了過濾器之外的所有內容相同,並更改過濾用戶的內容,那麼我將獲得所有OU中的所有用戶。 – 2013-04-04 14:11:33

+0

你確定這些對象是OU嗎? – JPBlanc 2013-04-04 16:58:03

+0

對象類被列爲:'top; organizationalUnit'。 – 2013-04-04 19:00:03

1

我使用類似這樣的東西。它通過路徑檢索字典名稱中的所有OU,只需正確更改SearchScope即可。

public Dictionary<string, string> GetOUInfo(SearchScope eSearchScope) 
    { 
     Dictionary<string, string> retValues = new Dictionary<string, string>(); 

     try 
     { 
      DirectoryEntry oDirectoryEntry = new DirectoryEntry("LDAP://myServer:1111/DC=myApp,DC=myDomain,DC=com", Username, Password); 
      DirectorySearcher oDirectorySearcher = new DirectorySearcher(oDirectoryEntry, 
       "(objectCategory=organizationalUnit)", null, eSearchScope); 

      SearchResultCollection oSearchResultCollection = oDirectorySearcher.FindAll(); 
      foreach (SearchResult item in oSearchResultCollection) 
      { 
       string name = item.Properties["name"][0].ToString(); 
       string path = item.GetDirectoryEntry().Path; 
       retValues.Add(path, name); 
      } 
     } 
     catch (Exception ex) 
     { 
     } 

     return retValues; 
    } 
0

使用此它將工作

PrincipalContext yourOU = new PrincipalContext(ContextType.Domain, "mycompany.com", "OU=Marketing,OU=Corporate,DC=mycompany,DC=com"); 
GroupPrincipal findAllGroups = new GroupPrincipal(yourOU, "*"); 
PrincipalSearcher ps = new PrincipalSearcher(findAllGroups); 
foreach (var group in ps.FindAll()) 
{ 
Console.WriteLine(group.DistinguishedName); 
}