试图通过SSL证书验证LDAP,无法达到服务器

发布于 2025-02-12 15:33:11 字数 1133 浏览 0 评论 0原文

我目前正在尝试使用以下VB.NET代码连接到LDAPS服务器,该代码应设置正确的参数,并使用下面的功能在调用绑定功能时验证证书。

ldaphost的值是IP,10.100.11.10,Ldapport的值为7636

connection = new LdapConnection(new LdapDirectoryIdentifier(LdapHost, LdapPort));
connection.AuthType = 2; // Negotiate
connection.SessionOptions.SecureSocketLayer = true;

connection.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback(VerifyServerCertificate);

//Both username and password are correct
connection.Credential = new System.Net.NetworkCredential(strUsername, strPassword); 
connection.Bind();

。 但是,在尝试验证服务器证书后,使用以下代码:

private bool VerifyServerCertificate(LdapConnection ldapConnection, X509Certificate certificate)
{
    try
    {
        X509Certificate2 certificate2 = new X509Certificate2(certificate);
        return certificate2.Verify();
    }
    catch (Exception ex)
    {
        throw new LdapException(9999, "Invalid certificate or path.");
    }
}

它在绑定函数上错误地说,它根本无法连接到LDAP服务器,并使用消息“ 无法达到LDAP服务器”。 尽管通过PowerShell测试了连接,但服务器可用。

我的验证方法有问题吗?我应该完全尝试其他方法吗?

I'm currently trying to connect to a LDAPS Server using the following VB.NET Code, which should set the right parameters and use the function seen below to verify the certificate when the Bind function is called.

The value of LdapHost is the IP, 10.100.11.10, and the value for LdapPort is 7636.

connection = new LdapConnection(new LdapDirectoryIdentifier(LdapHost, LdapPort));
connection.AuthType = 2; // Negotiate
connection.SessionOptions.SecureSocketLayer = true;

connection.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback(VerifyServerCertificate);

//Both username and password are correct
connection.Credential = new System.Net.NetworkCredential(strUsername, strPassword); 
connection.Bind();

This,
But upon trying to verify the Server Certificate, using the following code:

private bool VerifyServerCertificate(LdapConnection ldapConnection, X509Certificate certificate)
{
    try
    {
        X509Certificate2 certificate2 = new X509Certificate2(certificate);
        return certificate2.Verify();
    }
    catch (Exception ex)
    {
        throw new LdapException(9999, "Invalid certificate or path.");
    }
}

It Errors out at the Bind function saying that it cannot connect to the LDAP Server at all with the message "The LDAP Server cannot be reached"
Although upon testing the connection via PowerShell, the Server is available just fine.

Is there something wrong with my verification method? Should I try a different approach entirely?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2025-02-19 15:33:11

我发现验证不起作用的原因。
使用

 X509Chain chain = new X509Chain();

 X509Certificate2 certificate2 = new X509Certificate2(certificate);
 var chainBuilt = chain.Build(certificate2);
 LogEvent("Val", 0, "Chain building status: " + chainBuilt);
 if (chainBuilt == false) {
     foreach (X509ChainStatus chainStatus in chain.ChainStatus)
         LogEvent("Val", 0, "Chain error: " + chainStatus.Status + " " + chainStatus.StatusInformation);
     chain.Reset();
     return false;
 }  else  {
     chain.Reset();
     return true;
  }

如果验证失败,则可以帮助我了解根证书在该特定服务器上不信任。
此外,它告诉我,它无法到达撤销服务器以检查证书是否仍然有效。

这无法检查,因为配置使用 starttls证书,没有撤销服务器。

因此,我添加了

chain.ChainPolicy.VerificationFlags = X509VerificationFlags.IgnoreRootRevocationUnknown | X509VerificationFlags.IgnoreEndRevocationUnknown | X509VerificationFlags.IgnoreCtlSignerRevocationUnknown;

忽略有关撤销服务器的每个属性。现在可以按预期连接。

I have found the reason why the verification did not work.
Using

 X509Chain chain = new X509Chain();

 X509Certificate2 certificate2 = new X509Certificate2(certificate);
 var chainBuilt = chain.Build(certificate2);
 LogEvent("Val", 0, "Chain building status: " + chainBuilt);
 if (chainBuilt == false) {
     foreach (X509ChainStatus chainStatus in chain.ChainStatus)
         LogEvent("Val", 0, "Chain error: " + chainStatus.Status + " " + chainStatus.StatusInformation);
     chain.Reset();
     return false;
 }  else  {
     chain.Reset();
     return true;
  }

if the verification fails helped me understand that the Root Certificate was not trusted on that specific server.
Furthermore, it told me that it could not reach the Revokation Server to check if the Certificate is still valid.

This couldn't be checked though, since the configuration uses a StartTLS certificate, which does not have a Revokation Server.

Therefore, I added

chain.ChainPolicy.VerificationFlags = X509VerificationFlags.IgnoreRootRevocationUnknown | X509VerificationFlags.IgnoreEndRevocationUnknown | X509VerificationFlags.IgnoreCtlSignerRevocationUnknown;

to ignore every property regarding the Revokation Server. It can now connect as intended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文