2013-05-29 104 views
0

是否有任何直接獲取所有OU進入Active Directory的方法? 我曾嘗試下面的代碼,我在從活動目錄獲取所有OU

ouSearch.FindAll() 

越來越COM異常(操作無效)我的代碼如下所示。

public static List<string> GetAllOus(string ldapServer, string ldapUserName, string ldapPassWord) 
    { 

     List<string> orgUnits = new List<string>(); 
     string defaultNamingContext; 

     DirectoryEntry rootDSE = new DirectoryEntry(ldapServer + "/dc=server-dc,dc=com", ldapUserName, ldapPassWord, AuthenticationTypes.Anonymous); 
     //defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value.ToString(); 

     //DirectoryEntry defaultentry = new DirectoryEntry("LDAP://" + defaultNamingContext); 

     DirectorySearcher ouSearch = new DirectorySearcher(rootDSE, 
              "(objectClass=organizational-Unit)", 
              null, SearchScope.Subtree); 

     foreach (SearchResult resEnt in ouSearch.FindAll()) 
     { 
      string OUName = resEnt.GetDirectoryEntry().Name; 
      orgUnits.Add(OUName); 
     } 

     return orgUnits; 
    } 

請幫我解決這個問題。提前

感謝

+1

貴'ldapServer'有什麼樣的價值? –

+0

@marc_s:它包含值如LDAP://192.168.2.5(AD服務器) – MAC

+0

好吧,只要確保LDAP://部分是**大寫**(不要使用ldap ://''或'Ldap://'或類似的東西..) –

回答

2

使用(objectClass的= 組織單位),而不是(objectClass的=組織單元)。

+0

從我讀過的objectCategory中更快 –

相關問題