2013-05-29 65 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中更快 –

相关问题