2017-08-02 213 views
4

我想使用基于TLSv1.2和.NET Framework 4.5.2或4.6.2的LDAP来查询ActiveDirectory(但两者都有问题)。问题是它一直试图使用TLSv1.0,即使我使用“ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12”。使用c#和TLSv1.2连接到LDAP

“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服务器,我碰巧使用(多克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的可见交通表明,“版本”的“客户端内你好“数据包是”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) 
+0

我猜你可能已经看过这个http://blogs.perficient.com/microsoft/2016/04/ tsl-1-2-and-net-support /但是,如果你没有 –

+0

是的,我看过那个和其他人喜欢它。我已经尝试了TLS1.2应该工作的版本(.NET 4.6.2),并且还尝试了4.5解决方法“ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12”。这两种方法似乎都不适用于LDAP。 –

回答

0

添加几个注册表项,以启用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