从Active Directory获取所有OU

时间:2023-02-07 02:56:39

Is there any direct method for getting all OUs coming under in an Active Directory? I had tried the following code, and i am getting Com Exception (Invalid operation) at

有没有直接的方法让所有OU都在Active Directory中?我尝试了下面的代码,我得到了Com Exception(无效操作)

ouSearch.FindAll()

My code is shown below.

我的代码如下所示。

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;
    }

Please help me to resolve this issue.

请帮我解决这个问题。

Thanks in advance

提前致谢

1 个解决方案

#1


2  

use (objectClass=organizationalUnit) instead of (objectClass=organizational-Unit)

使用(objectClass = organizationalUnit)而不是(objectClass = organizational-Unit)

#1


2  

use (objectClass=organizationalUnit) instead of (objectClass=organizational-Unit)

使用(objectClass = organizationalUnit)而不是(objectClass = organizational-Unit)