使用c#和TLSv1.2连接到LDAP

时间:2017-08-02 17:25:09

标签: c# .net active-directory ldap tls1.2

我想使用LDAP over TLSv1.2和.NET Framework 4.5.2或4.6.2查询ActiveDirectory(但两者都有问题)。问题是它一直在尝试使用TLSv1.0,即使我使用的是“ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12”。

“System.DirectoryServices.Protocols”是否可用于通过TLSv1.2查询LDAP?如果是这样,启用该TLS版本的正确方法是什么?

最终,我想从Web API 2控制器执行此操作,但作为重现问题的简单测试,我有以下控制台应用程序:

using System;
using System.Diagnostics;
using System.DirectoryServices.Protocols;
using System.Net;

namespace Ldap
{
  class Program
  {
    private const string ldapHost = "169.254.212.120";
    private const int ldapPort = 30389; // normally just 389
    private const int ldapSslPort = 30636; // normally just 636
    private const bool sslEnabled = true;
    private const string userBaseDistinguishedName = "dc=example,dc=org";
    private const string bindUserCommonName = "admin";
    private const string bindUserDistinguishedName = "cn=admin,dc=example,dc=org";
    private const string bindUserPassword = "admin";

    static void Main(string[] args)
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        using (LdapConnection connection = CreateConnection())
        {
            try
            {
                connection.Bind();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
    }

    private static LdapConnection CreateConnection()
    {
        var directoryIdentifier = new LdapDirectoryIdentifier(ldapHost,
            sslEnabled ? ldapSslPort : ldapPort, true, false);

        var credential = new NetworkCredential(bindUserDistinguishedName, bindUserPassword);

        var conn = new LdapConnection(directoryIdentifier, credential, AuthType.Basic);

        conn.SessionOptions.SecureSocketLayer = sslEnabled;
        conn.SessionOptions.ProtocolVersion = 3; // Use LDAPv3 (otherwise it appears to default to LDAPv2)

        return conn;
    }
  }
}

对于服务器,我正在使用OpenLdap docker容器(暴露端口30389和30636,而不是标准端口)对此进行测试,尽管最终此代码将用于连接&查询ActiveDirectory。

为了站起来测试LDAP服务器,我碰巧使用(Docker 17.06 CE):

docker run --name test_ldap -p 0.0.0.0:30636:636 -p 0.0.0.0:30389:389 --env LDAP_TLS_CIPHER_SUITE="SECURE256:+SECURE128:-VERS-TLS-ALL:+VERS-TLS1.2:-RSA:-DHE-DSS:-CAMELLIA-128-CBC:-CAMELLIA-256-CBC" --env LDAP_TLS_VERIFY_CLIENT="allow" --hostname example.org --detach osixia/openldap:1.1.7

同时运行Wireshark:Wireshark中显示的流量显示“Client Hello”数据包中的“Version”为“TLS 1.0(0x0301)”。

打开的LDAP服务器中的日志显示:

59810624 conn=1002 fd=16 ACCEPT from IP=172.17.0.1:44606 (IP=0.0.0.0:636)
TLS: can't accept: An unknown public key algorithm was encountered..
59810624 conn=1002 fd=16 closed (TLS negotiation failure)

1 个答案:

答案 0 :(得分:0)

添加几个注册表项以启用TLS 1.2;以下PowerShell脚本添加它们:

def replace_names_array(name_to_replace, new_name)
  @@names_array = @@names_array.map do |name|
    name == name_to_replace ? new_name : name
  end
end

我在此处找到了此信息:https://docs.microsoft.com/en-us/windows-server/identity/ad-fs/operations/manage-ssl-protocols-in-ad-fs