【问题标题】:Search Active Directory for an OU using a partial path to the OU使用 OU 的部分路径在 Active Directory 中搜索 OU
【发布时间】:2011-05-03 16:46:48
【问题描述】:

在 AD Query 语法中有没有办法通过搜索其部分路径来找到 OU 的完整路径?

例如,我的 OU 的完整路径是:

OU=Clerks,OU=OfficeA,OU=Administration,DC=domain,DC=local

现在,我想尝试使用部分路径搜索并找到该对象:

OU=Clerks,OU=OfficeA

我希望能够搜索以下内容:

(&(objectCategory=organizationalUnit)(path=Clerks/OfficeA*))

我找不到任何有关如何完成此类操作的语法示例。我正在开发的程序要求我获得许多 OU 的路径,这些 OU 在最后两级 OU 中都有一个共同的结构,但是它们可以嵌套在域中的任何给定深度。如果我可以像这样搜索,那么只需通过最后两个 OU 嵌套级别搜索即可轻松获得完整路径。

【问题讨论】:

    标签: c# active-directory directory ldap directoryservices


    【解决方案1】:

    您想要做的事情存在于纯 LDAP 实现中,它是一个名为 ExtensibleMatch 的功能,似乎在 this wiki article 中得到了正确解释。你还会发现一些有用的例子here

    但它不存在于 Active-Directory 中

    所以这是一个用 C# 编写的方法,它利用了 DirectoryEntryParent 属性。

       static List<DirectoryEntry> OuInTheFormOf(DirectoryEntry deBase, string ou1, string ou2)
        {
          List<DirectoryEntry> deList = null;
    
          /* Directory Search
           */
          DirectorySearcher dsLookFor = new DirectorySearcher(deBase);
          dsLookFor.Filter = ou1;
          dsLookFor.SearchScope = SearchScope.Subtree;
          dsLookFor.PropertiesToLoad.Add("ou");
    
          SearchResultCollection srcOUs = dsLookFor.FindAll();
    
          if (srcOUs.Count != 0)
          {
            deList = new List<DirectoryEntry>();
    
            foreach (SearchResult srOU in srcOUs)
            {
              DirectoryEntry deOU = srOU.GetDirectoryEntry();
              if (deOU.Parent.Name.ToUpper() == ou2.ToUpper())
                deList.Add(deOU);
            }
          }
          return deList;
        }
    

    这里是用法:

      /* Connection to Active Directory
       */
      DirectoryEntry deBase = new DirectoryEntry("LDAP://WM2008R2ENT:389/dc=dom,dc=fr");
    
      List<DirectoryEntry> l = OuInTheFormOf(deBase, "ou=Clerks", "ou=OfficeA");
    
      foreach (DirectoryEntry deTmp in l)
      {
        Console.WriteLine(deTmp.Properties["distinguishedName"].Value);
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 2019-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多