使用“DirectoryEntry”API从远程计算机通过ssl连接到LDAP

时间:2015-07-02 12:40:33

标签: c# ssl openldap directoryentry

我需要从远程计算机连接到LDAP over SSL;对此有很多讨论,但其中发布的解决方案只能在同一个Windows Server域机器上运行;相同的代码无法从远程计算机成功连接;

我试过的代码:

DirectoryEntry entry = new DirectoryEntry("LDAP://fqdn:636/DC=aa,DC=bb", "username", "password");
DirectorySearcher searcher = new DirectorySearcher();
searcher.SearchRoot = entry;
searcher.SearchScope = SearchScope.Subtree;
searcher.Filter = "(&(objectCategory=person)(objectClass=user))";
SearchResultCollection results = searcher.FindAll();

再说一遍,我可以从同一个WindowsServer域机器获得结果;但是当我从远程计算机上尝试相同的代码时,我得到异常“服务器无法运行”;

我发现它是由于证书验证失败,即Windows Server的证书未在远程机器中验证;但我不知道如何覆盖'DirectoryEntry对象'的证书验证;

  • post有证书问题解决方案;但它的'LdapConnection'

  • 但我需要 DirectoryEntry API的解决方案;

仅供参考;这个post对Java提出了同样的问题而没有答案;我的要求是 C#

1 个答案:

答案 0 :(得分:2)

不幸的是,DirectoryEntry没有可用的证书覆盖。

您必须使用System.DirectoryServices.Protocols命名空间中的较低级LdapConnectionLdapDirectoryIdentifier类。

转换代码应该不会太难,并且这样做,您将能够处理本地和远程目录资源。我刚才遇到了同样的问题,我写了一篇关于为什么会出现这种情况的广泛帖子,以及如何解决它 - 检查出来:

Set callback for System.DirectoryServices.DirectoryEntry to handle self-signed SSL certificate?