获取Active Directory中组织单位的父组织单位(如果有)

时间:2011-03-23 02:09:08

标签: c# active-directory directoryservices

我创建了一个小实体类:

public class OrganizationalUnit
{
    public string Name { get; set; }
    public string ParentUO { get; set; }
    public string Path { get; set; }    
}

以下是我创建此类对象的方法:

    /// <summary>
    /// Provides an object that allows you to get organizational units within an 
    /// active directory domain.
    /// </summary>
    /// <param name="connectionString">The LDAP connection string to a domain. 
    /// For example LDAP://DC=YourCompany,DC=com</param>
    public ActiveDirectoryOrganizationalUnitRepository(string connectionString, string username, string password)
    {
        organizationalUnits = new List<OrganizationalUnit>();

        if (DomainExists(connectionString))
        {
            var baseDirectory = new DirectoryEntry(connectionString);
            baseDirectory.Username = username;
            baseDirectory.Password = password;

            DirectorySearcher searcher = new DirectorySearcher();
            searcher.SearchRoot = baseDirectory;
            searcher.Filter = "(objectCategory=organizationalUnit)";
            searcher.SearchScope = SearchScope.Subtree;

            var ouResults = searcher.FindAll();

            foreach (SearchResult ou in ouResults)
            {
                organizationalUnits.Add(new OrganizationalUnit() { 
                                        Path = ou.Path, 
                                        Name = ou.Properties["name"][0].ToString(),
                                        ParentUO = ou.Properties["parent"][0].ToString()}
                );                    
            }
        }        
    }

我需要填写ParentOU属性的帮助。在尝试获取时,我得到一个超出范围异常的索引:

ParentUO = ou.Properties["parent"][0].ToString();

所以这意味着,没有名为'parent'的属性。

有什么建议吗?我还想找到一个存在的属性列表,但我还没有在网上找到一个。

3 个答案:

答案 0 :(得分:2)

首先: 从纯粹的目录点vue,您在organizationalUnit(OU)中有一个名为“distinguishedName”的属性,如下所示:

OU=currentOU,OU=parentOU,...,DC=domain,DC=..

这样您就可以轻松计算父OU的字符串。

第二名: 在编程的角度来看,你有一个DirectoryEntry类的属性parent。这是一个示例代码。

String myADSPath = "LDAP://onecity/CN=user,CN=Users,DC=onecity,DC=corp,DC=fabrikam,DC=com";
DirectoryEntry myDirectoryEntry=new DirectoryEntry(myADSPath, UserName, SecurelyStoredPassword);

Console.WriteLine("Parent is :"+myDirectoryEntry.Parent.Path);

JP

答案 1 :(得分:0)

我发现此代码列出了SearchResult对象可能具有的所有属性。它似乎没有列出它的父组织单位。

DirectorySearcher searcher = new DirectorySearcher();

searcher.SearchRoot = baseDirectory;
searcher.Filter = "(objectCategory=organizationalUnit)";
searcher.SearchScope = SearchScope.Subtree;

var ouResults = searcher.FindAll();

foreach (SearchResult ou in ouResults)
{

    ResultPropertyCollection myResultPropColl;
    myResultPropColl = ou.Properties;
    Console.WriteLine("The properties of the " +
            "'mySearchResult' are :");

    foreach (string myKey in myResultPropColl.PropertyNames)
    {
        string tab = "    ";
        Console.WriteLine(myKey + " = ");
        foreach (Object myCollection in myResultPropColl[myKey])
        {
            Console.WriteLine(tab + myCollection);
        }
    }
}

答案 2 :(得分:0)

您是否尝试过DirectoryEntry.Parent

以下代码应该有效,但我没有尝试过。

organizationalUnits.Add(new OrganizationalUnit() { 
                                    Path = ou.Path, 
                                    Name = ou.Properties["name"][0].ToString(),
                                    ParentUO = ou.GetDirectoryEntry().Parent.Path}

organizationUnit类对象上没有名为“Parent”的属性。有关组织单位的所有属性,请查看MSDN