【问题标题】:Getting the parent organization unit (if any) of an organizational unit in Active Directory获取 Active Directory 中组织单位的父组织单位(如果有)
【发布时间】:2018-06-01 18:04:16
【问题描述】:

我创建了一个小型实体类:

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();

所以这意味着,没有名为“父”的属性。

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

【问题讨论】:

    标签: c# active-directory directoryservices


    【解决方案1】:

    第一: 从 vue 的纯目录点来看,您的组织单元 (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);
    

    日本

    【讨论】:

      【解决方案2】:

      我发现这段代码列出了 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);
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        你试过DirectoryEntry.Parent吗?

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多