繁体   English   中英

通过System.DirectoryServices将StartTLS与LDAP一起使用

[英]Using StartTLS with LDAP from System.DirectoryServices

我试图连接到需要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一起使用?

过去在野外通常存在许多细微的LDAP堆栈不兼容问题,这些不兼容问题仍可能适用于客户可能正在使用的潜在旧有方案。

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

显然,更新OpenLDAP和/或Windows(当然最好同时更新)应该可以解决这些问题,如果它们确实是造成这种情况的罪魁祸首。

祝好运!

请阅读本主题: 通过TLS / SSL加密连接进行绑定

例子19.使用基本身份验证和SSL / TLS绑定到安全端口50001上的ADAM实例

string hostNameAndSSLPort = "sea-dc-02.fabrikam.com:50001";
string userName = "cn=User1,cn=AdamUsers,cn=ap1,dc=fabrikam,dc=com";
string password = "adamPassword01!";

// establish a connection
LdapConnection connection = new LdapConnection(hostNameAndSSLPort);

// create an LdapSessionOptions object to configure session 
// settings on the connection.
LdapSessionOptions options = connection.SessionOptions;

options.ProtocolVersion = 3;

options.SecureSocketLayer = true;

connection.AuthType = AuthType.Basic;

NetworkCredential credential =
        new NetworkCredential(userName, password);

connection.Credential = credential;

try
{
    connection.Bind();
    Console.WriteLine("\nUser account {0} validated using " +
        "ssl.", userName);

    if (options.SecureSocketLayer == true)
    {
        Console.WriteLine("SSL for encryption is enabled\nSSL information:\n" +
        "\tcipher strength: {0}\n" +
        "\texchange strength: {1}\n" +
        "\tprotocol: {2}\n" +
        "\thash strength: {3}\n" +
        "\talgorithm: {4}\n",
        options.SslInformation.CipherStrength,
        options.SslInformation.ExchangeStrength,
        options.SslInformation.Protocol,
        options.SslInformation.HashStrength,
        options.SslInformation.AlgorithmIdentifier);
    }

}
catch (LdapException e)
{
    Console.WriteLine("\nCredential validation for User " +
        "account {0} using ssl failed\n" +
        "LdapException: {1}", userName, e.Message);
}
catch (DirectoryOperationException e)
{
    Console.WriteLine("\nCredential validation for User " +
    "account {0} using ssl failed\n" +
    "DirectoryOperationException: {1}", userName, e.Message);
}

下一个示例显示“如何使用TLS进行身份验证和执行任务”

string hostOrDomainName = "fabrikam.com";
string userName = "user1";
string password = "password1";

// establish a connection to the directory
LdapConnection connection = new LdapConnection(hostOrDomainName);

NetworkCredential credential =
    new NetworkCredential(userName, password, domainName);

connection.Credential = credential;

connection.AuthType = AuthType.Basic;

LdapSessionOptions options = connection.SessionOptions;

options.ProtocolVersion = 3;

try
{
    options.StartTransportLayerSecurity(null);
    Console.WriteLine("TLS started.\n");
}
catch (Exception e)
{
    Console.WriteLine("Start TLS failed with {0}", 
        e.Message);
    return;
}

try
{
    connection.Bind();
    Console.WriteLine("Bind succeeded using basic " +
        "authentication and SSL.\n");

    Console.WriteLine("Complete another task over " +
        "this SSL connection");
    TestTask(hostName);
}
catch (LdapException e)
{
    Console.WriteLine(e.Message);
}

try
{
    options.StopTransportLayerSecurity();
    Console.WriteLine("Stop TLS succeeded\n");
}
catch (Exception e)
{
    Console.WriteLine("Stop TLS failed with {0}", e.Message);
}

 Console.WriteLine("Switching to negotiate auth type");
 connection.AuthType = AuthType.Negotiate;

 Console.WriteLine("\nRe-binding to the directory");
 connection.Bind();

// complete some action over this non-SSL connection
// note, because Negotiate was used, the bind request 
// is secure. 
// run a task using this new binding
TestTask(hostName);

在此问题上进行了更多工作之后,我发现我遇到了几个问题:

  1. 在我们的测试套件(doh!)中连接到AD时,代码中存在一个错误,其中端口号被错误地更改为SSL端口(636)。
  2. OpenLDAP测试服务器(是我们客户的副本)正在使用openldap-2.4.18-StartTLS存在已知问题。

在将补丁应用到OpenLDAP之后(如此处所讨论-http: //www.openldap.org/lists/openldap-bugs/200405/msg00096.html ),我们能够修复#2-这时我们开始遇到另一个错误“发生本地错误”。

虽然最初我们有以下代码:

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

我们在测试时已将其删除,并且由于OpenLDAP服务器使用的是自签名证书,因此不在受信任的存储区中。 重新引入该回调解决了此问题,尽管我们现在将其设为可配置的选项,即“验证服务器证书是/否”,因此客户需要选择跳过检查(主要是供我们的质量检查小组使用)。

感谢Steffen向我指出了引导我使用该解决方案的OpenLDAP版本的方向。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM