c# - 为什么我的 LDAP 查询失败?

标签 c# active-directory ldap

在我的开发机器上,我有一系列虚拟机。其中之一是域 Controller 。域 Controller 确实在工作,因为我无法在未经身份验证的情况下登录到其他 VM。

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

我的域 Controller 树看起来像:

  • DC 机器名称 = ESDEV-DC01
  • 事件目录名称 = ESDEV.COM
  • 目标节点的规范名称 = ESDEV.COM/Users

我的子树目标看起来像:

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

我的参数是:

  • DirectoryPath = "LDAP://OU=Users, DC=ESDEV-DC01,DC=ESDEV,DC=Com"
  • SearchFilter = "(&(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();
        }
    }
}

最佳答案

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

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

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

LDAP 前缀必须全部大写

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

您可以使用 PrincipalSearcher 和“query-by-example”主体进行搜索:

// 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它很好地展示了如何充分利用 System.DirectoryServices.AccountManagement 中的新功能。或者查看 MSDN documentation on the System.DirectoryServices.AccountManagement命名空间。

关于c# - 为什么我的 LDAP 查询失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19277885/

相关文章:

active-directory - 我可以模拟通过表单例份验证的客户端并建立与 SQL Server 的受信任连接吗?

c# - 替换 proxyAddresses 值

windows - 系统错误1722。RPC服务器不可用。调用网络用户时

java - 此代码中的 LDAP 连接泄漏

c# - 我什么时候需要在 C# 中调用 base()?

c# - 在我的应用程序中如何关闭 Winword.exe

c# - 是否可以创建通用 API GET 操作?

docker - 在 Sonarqube Docker 容器中设置 LDAPS 登录时遇到问题

ssl - 为 LDAPS 通信删除证书时 JNDI 调用不会失败

c# - RazorEngine:不解析出现在小于号 ("<"之后的变量)