【问题标题】:Connect to LDAP using c# and TLSv1.2使用 c# 和 TLSv1.2 连接到 LDAP
【发布时间】:2018-01-10 01:30:21
【问题描述】:

我想使用 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”数据包中的“版本”是“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)

【问题讨论】:

  • 我猜你可能已经看过这个blogs.perficient.com/microsoft/2016/04/tsl-1-2-and-net-support 但以防万一你没有
  • 是的,我看过那个,其他人也喜欢。我已经尝试了 TLS1.2 应该工作的版本(.NET 4.6.2),并且还尝试了 4.5 解决方法“ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12”。这两种方法似乎都不适用于 LDAP。

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


【解决方案1】:

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

New-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client' -Force | Out-Null
New-ItemProperty -path 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client' -name 'Enabled' -value '1' -PropertyType 'DWord' -Force | Out-Null
New-ItemProperty -path 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client' -name 'DisabledByDefault' -value 0 -PropertyType 'DWord' -Force | Out-Null

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

【讨论】:

    猜你喜欢
    • 2021-08-01
    • 2010-11-29
    • 2018-08-16
    • 2020-09-29
    • 2022-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多