2

在我的开发机器上,我有一系列 VM。其中之一是域控制器。域控制器确实在工作,因为如果不对其进行身份验证,我就无法登录到其他 VM。

我正在尝试针对此 DC 测试 LDAP 查询,但一直失败

我的域控制器树看起来像:

  • 直流机器名称= ESDEV-DC01
  • 活动目录名称 =ESDEV.COM
  • 目标节点的规范名称 =ESDEV.COM/Users

我的子树目标看起来像:

  • 属性名称 = objectCategory
  • 属性值 = CN=Person,CN=Schema,CN=Configuration,DC=ESDEV,DC=COM

我的参数是:

  • 目录路径 ="LDAP://OU=Users, DC=ESDEV-DC01,DC=ESDEV,DC=Com"
  • 搜索过滤器 ="(&(objectCategory=Person))"

问题:

我不断收到“服务器上没有这样的对象”。

  1. 这是否意味着它正在查找服务器目录?
  2. 为什么查询失败?
  3. LDAP 查询是否区分大小写?

我的控制台应用程序代码如下所示:

我认为没有这篇文章可以回答我的问题,但是对于那些关心我用来测试查询的代码的人......

namespace LDAPQueryTester
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string directoryPath = ConfigurationManager.AppSettings["DirectoryPath"];
                string searchFilter = ConfigurationManager.AppSettings["SearchFilter"];

                DirectoryEntry rootEntry = new DirectoryEntry(directoryPath);
                DirectorySearcher srch = new DirectorySearcher(rootEntry);

                srch.SearchScope = SearchScope.Subtree;

                if (searchFilter.Length > 0)
                {
                    srch.Filter = searchFilter;
                }

                SearchResultCollection res = srch.FindAll();

                if (res.Count <= 0)
                {
                    Console.WriteLine("Your query did NOT return results");
                }
                else
                {
                    Console.WriteLine("Your query returned results");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            Console.ReadLine();
        }
    }
}
4

1 回答 1

2

据我记得,这Users是一个通用容器——不是 OU——所以你应该试试这个 LDAP 路径:

LDAP://CN=Users,DC=ESDEV-DC01,DC=ESDEV,DC=Com

注意:CN=Users而不是OU=Users.

并且LDAP前缀必须全部大写

但是,如果您使用的是 .NET 3.5 或更高版本,我建议您查看新的System.DirectoryServices.AccountManagement命名空间,它可以让很多东西更容易使用!

您可以使用 aPrincipalSearcher和“按示例查询”主体进行搜索:

// create your domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "ESDEV.COM", "CN=Users, DC=ESDEV-DC01,DC=ESDEV,DC=Com"))
{
   // define a "query-by-example" principal - here, we search for a UserPrincipal 
   UserPrincipal qbeUser = new UserPrincipal(ctx);

   // create your principal searcher passing in the QBE principal    
   PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

   // find all matches
   foreach(var found in srch.FindAll())
   {
       // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
   }
}

如果您还没有 - 绝对阅读 MSDN 文章Managing Directory Security Principals in the .NET Framework 3.5,它很好地展示了如何充分利用 .NET Framework 中的新功能System.DirectoryServices.AccountManagement。或查看System.DirectoryServices.AccountManagement命名空间上的 MSDN 文档。

于 2013-10-09T20:59:04.410 回答