我想在 TLSv1.2 和 .NET Framework 4.5.2 或 4.6.2 上使用 LDAP 查询 ActiveDirectory(但两者都有问题)。问题是它一直尝试使用 TLSv1.0,即使我使用的是“ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12”。
“System.DirectoryServices.Protocols”是一个我可以用来通过 TLSv1.2 查询 LDAP 的包吗?如果是这样,启用该 TLS 版本的正确方法是什么?
最终,我想从 Web API 2 Controller 执行此操作,但作为重现问题的简单测试,我有以下控制台应用程序:
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)
最佳答案
添加几个注册表项以启用 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://learn.microsoft.com/en-us/windows-server/identity/ad-fs/operations/manage-ssl-protocols-in-ad-fs
关于c# - 使用 c# 和 TLSv1.2 连接到 LDAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45467336/