【发布时间】:2015-02-19 14:11:39
【问题描述】:
我正在尝试从 LDAP 获取根 DirectoryEntry,以便显示它的漂亮图形树视图。
在正常连接下一切正常,但我无法使用 SSL。
var root = this.checkBoxSSL.Checked
? new DirectoryEntry("LDAP://" + this.textBoxServer.Text,
this.textBoxUsername.Text,
this.textBoxPassword.Text,
AuthenticationTypes.SecureSocketsLayer)
: new DirectoryEntry("LDAP://" + this.textBoxServer.Text,
this.textBoxUsername.Text,
this.textBoxPassword.Text);
var dn = root.Properties["distinguishedName"].Value;
等等……
但我收到“服务器无法运行”异常。这一切似乎都归结为绑定过程。根据互联网研究,这可能是证书和/或身份验证方法(NTLM 等)的问题。
那么如何通过 SSL 获得有效的 DirectoryEntry?
我愿意接受替代解决方案,只要我可以检索我需要的节点的所有 LDAP 属性。 (根、DC、OU、CN、组和用户)
编辑: 看来问题归结为 SSL 证书。我们只有一个自签名证书 atm。这似乎被.NET默认拒绝了。稍后我们将使用正确签名的证书尝试此操作,但我可能也需要能够处理自签名证书。
这是我对证书的了解有限的地方。我目前正在探索一种不同的代码解决方案,因为它似乎是唯一允许我影响整个证书处理的解决方案:
System.Security.Cryptography.X509Certificates.X509Certificate2 cert = new System.Security.Cryptography.X509Certificates.X509Certificate2();
cert.Import("..\\..\\test certificate.cer");
LdapConnection con = new LdapConnection("ip:636");
con.Credential = new NetworkCredential("un", "pw");
con.AuthType = AuthType.Ntlm;
con.SessionOptions.SecureSocketLayer = true;
con.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback((ldapcon, cer) => {
var cer2 = new System.Security.Cryptography.X509Certificates.X509Certificate2(cer);
StringBuilder strb = new StringBuilder();
strb.AppendFormat("{0} {1} matches: {2}\n", "Subject", cert.Subject, cert.Subject.Equals(cer2.Subject));
strb.AppendFormat("{0} {1} matches: {2}\n", "Cert Hash", cert.GetCertHashString(), Enumerable.SequenceEqual<byte>(cer.GetCertHash(), cert.GetCertHash()));
strb.AppendFormat("{0} matches: {2}\n", "Public Key", cert.GetPublicKeyString(), Enumerable.SequenceEqual<byte>(cer.GetPublicKey(), cert.GetPublicKey()));
strb.AppendFormat("{0}: {1}, {2}", "Verification", cert.Verify(), cer2.Verify());
var res = MessageBox.Show(strb.ToString(),
"Allow certificate?", MessageBoxButtons.YesNo);
return res == System.Windows.Forms.DialogResult.Yes;
});
con.Bind();
基本上,如果 VerifyServerCertificateCallback 返回 true,则连接成功,如果返回失败,则连接失败,出现与我尝试过的任何其他解决方案相同的异常。
奇怪的是,安装 AD 证书或 AD 控制器的根证书都没有帮助其他解决方案,但它确实改变了 Verify() 方法的结果。
我必须对回调中的证书执行哪些检查以维护 SSL 连接的安全性?
【问题讨论】:
-
我也有同样的问题!你有没有设法解决这个问题?
-
@Markus 是的,我或多或少做到了。但由于我不再在那里工作,我将不得不凭记忆做出回应。本质上,事实证明我必须使用两个不同的 LDAP 库,具体取决于我是否需要 SSL。我无法让 LdapConnection 与 SSL 一起使用,并且较新的 .net 库拒绝在非 SSL 中工作。
-
好的,“新的”.net 库是您上一个示例中的
LDAPConnection吗? -
@Markus:对。 LDAPConnection 是新的,DirectoryEntry 是旧的。
标签: c# ssl active-directory