【问题标题】:C# Cannot connect to AD using LDAPSC# 无法使用 LDAPS 连接到 AD
【发布时间】:2022-06-30 14:32:01
【问题描述】:

我的要求是更改AD的用户密码。于是,我通过https://bl.ocks.org/magnetikonline/0ccdabfec58eb1929c997d22e7341e45成功在AD域服务器上创建了LDAP SSL安全连接。

使用ldp.exe 工具(在同一个AD 服务器上)我能够连接SSL。这意味着在 AD 服务器上启用了 LDAPS。

现在我正在尝试使用位于客户端的库 Novell.Directory.Ldap 使用以下代码从 ASP.NET Core 应用程序连接它:

public LdapConnection GetLDAPConnection(IOptions<ADConfiguration> _settings)
{
    LdapConnection connection = new LdapConnection { SecureSocketLayer = true };
    connection.Connect(_settings.Value.DomainIPAddress, _settings.Value.Port); //port is 636
    connection.Bind(_settings.Value.AdminDn, _settings.Value.Password);

    if (connection.Bound)
    {
        return connection;
    }

    return null;
}

Connect 方法抛出此错误:

System.Security.Authentication.AuthenticationException: '远程证书被提供的 RemoteCertificateValidationCallback 拒绝。'

客户端机器是否也有 SSL 设置?或者我还缺少什么?请帮忙

【问题讨论】:

  • 如果你使用 ASP.NET Core,你应该看看System.DirectoryServicesSystem.DirectoryServices.Protocols,这似乎与你的Novell.Directory.Ldap 很接近。 @Gabriel-Luci 已经回答了。 System.DirectoryServices.Protocols 中的 LdapConnection.SessionOptions.VerifyServerCertificate 可以帮助您验证证书。回调只需要返回true
  • System.DirectoryServices 仅支持窗口。我想要跨平台。
  • System.DirectoryServices.Protocols 是跨平台的。

标签: asp.net-core active-directory ldap ssl-certificate ldapconnection


【解决方案1】:

我怀疑你的问题是使用域控制器的 IP 地址:_settings.Value.DomainIPAddress

SSL/TLS 有两个目的:加密流量,以及验证服务器实际上是您要与之交谈的服务器。为了解决第二个目的,您用于连接的域名必须与证书中的域名匹配。在您的情况下,当它验证证书时,它会看到您连接到,比如说,10.0.0.1,但它从服务器获得的证书说它是example.com,并且验证失败,因为它不匹配。

您必须:

  1. _settings.Value.DomainIPAddress更改为证书中使用的域名。如果您没有为该域名设置 DNS,您可以在 hosts 文件中添加一个条目。
  2. 告诉LdapConnection 忽略证书错误。数据仍将被加密,但不会验证证书(域不匹配、证书过期等)。不建议将其用于生产应用程序,但这里有一个示例:https://stackoverflow.com/a/67818854/1202807

【讨论】:

  • 将此库 Novell.Directory.Ldap 用于选项 1,如果我尝试使用域名连接,则会返回错误 SocketException: No such host is known. 。即使没有 SSL 连接,我也会收到此错误。对于 2,我尝试了 connection.UserDefinedServerCertValidationDelegate += (sender, certificate, chain, errors) =&gt; true; 它在没有验证证书的情况下成功运行。
  • @Varsh "No such host is known" 表示 DNS 问题。域名无法解析为 IP 地址。
【解决方案2】:

下面的代码可以让我使用 LDAPS 连接到 AD

ldapConnection = new LdapConnection(new LdapDirectoryIdentifier("your.LDAPSserver.com", 636));

var networkCredential = new NetworkCredential("UsernameWithoutDomain", "yourPassword", "AD.yourDOMAIN.com");
ldapConnection.SessionOptions.SecureSocketLayer = true;
ldapConnection.SessionOptions.ProtocolVersion = 3;
ldapConnection.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback(ServerCallback);
ldapConnection.AuthType = AuthType.Negotiate;
ldapConnection.Bind(networkCredential);
        
SearchRequest Srchrequest = new SearchRequest("CN=Users,DC=AD,DC=YOURCOMPANY,DC=COM", "mail=useremail@company.com", System.DirectoryServices.Protocols.SearchScope.Subtree);
SearchResponse SrchResponse = (SearchResponse)ldapConnection.SendRequest(Srchrequest);

// 服务器回调

private static bool ServerCallback(LdapConnection connection, X509Certificate certificate)
{
    return true;
}

令人惊讶的是,当我不使用 networkCredential 并且仅使用 ldapConnection.Bind(); 时它也可以工作,似乎它在我的本地计算机上使用我的本地凭据作为默认凭据.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-20
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多