将StartTLS与System.DirectoryServices中的LDAP一起使用

时间:2012-01-18 03:31:42

标签: c# ldap directoryservices starttls

我正在尝试连接到需要StartTLS的LDAP服务器,但没有运气 - 每当我使用SessionOptions.StartTransportLayerSecurity(..)或将SessionOptions.SecureSocketLayer设置为true时,我都会遇到异常。

这是我正在使用的代码:

using (var connection = new LdapConnection(new LdapDirectoryIdentifier(config.LdapServer, config.Port, false, false)))
{
    connection.SessionOptions.ProtocolVersion = 3;
    connection.Credential = new NetworkCredential(config.BindDN, config.BindPassword);
    connection.SessionOptions.VerifyServerCertificate += (conn, cert) => {return true;};
    connection.AuthType = AuthType.Basic;
    //connection.SessionOptions.SecureSocketLayer = true;
    connection.SessionOptions.StartTransportLayerSecurity(null); // throws here, same if done after bind.
    connection.Bind();

    ... do stuff with connection
}

生成的异常是“TlsOperationException:发生了未指定的错误”,这在调用StartTransportLayerSecurity方法时会发生。

我已针对OpenLDAP服务器和Active Directory两者测试了代码,但两者均无效。

有谁知道如何让StartTLS与System.DirectoryServices一起使用?

2 个答案:

答案 0 :(得分:3)

以前存在大量微妙的LDAP堆栈不兼容性,这仍然可能适用于您的客户可能使用的潜在遗留方案。

以下是有关OpenLDAP与Microsoft的LDAP堆栈之间不兼容的最常见问题(我将在有更多信息后修改和/或替换这些链接)

显然,更新OpenLDAP和/或Windows(理想情况下都是两者)应该解决这些问题,如果它们在这里成为罪魁祸首。

祝你好运!

答案 1 :(得分:2)

在这个问题上做了一些工作后,我发现我遇到了几个问题:

  1. 在我们的测试套件(doh!)中连接到AD时,代码中的端口号错误地更改为SSL端口(636)。
  2. OpenLDAP测试服务器(我们客户的副本)使用的是openldap-2.4.18 - 已知StartTLS存在问题。
  3. 在向OpenLDAP应用补丁后(如此处所述 - http://www.openldap.org/lists/openldap-bugs/200405/msg00096.html)我们能够修复#2 - 此时我们开始收到另一个错误“发生本地错误”。

    虽然最初我们有这个代码:

    connection.SessionOptions.VerifyServerCertificate 
        += (conn, cert) => {return true;};
    

    我们在测试时删除了它,并且因为OpenLDAP服务器使用的是自签名证书,而不是在受信任的商店中。重新引入该回调解决了这个问题,尽管我们现在将其作为可配置选项,即“验证服务器证书Y / N”,因此客户需要选择跳过支票(主要是我们的QA团队使用)。

    感谢Steffen指出我的OpenLDAP版本的方向,这引导我找到这个解决方案。

相关问题