我们有一个应用程序,它使用LDAP,通过IP地址,通过VPN隧道,使用以下代码对远程AD进行身份验证:
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, ldap.Host, ldap.Path.Replace("/", ""), ContextOptions.Negotiate, UserName, Password))
{
using (UserPrincipal user = UserPrincipal.FindByIdentity(pc, domainAndUsername))
{
if (user != null)
{
SAMAccountName = user.SamAccountName;
retVal = true;
}
}
}这对于正常的、非SSL的LDAP非常有用。但是,我们遇到了一种情况,即我们需要通过SSL连接到LDAPS,但它不起作用。我尝试了大量关于PrincipalContext构造函数的变体,但是我们所做的每一件事都会导致连接失败,从而导致以下错误:
System.DirectoryServices.AccountManagement.PrincipalServerDownException: The server could not be contacted. ---> System.DirectoryServices.Protocols.LdapException: The LDAP server is unavailable.
at System.DirectoryServices.Protocols.LdapConnection.Connect()
at System.DirectoryServices.Protocols.LdapConnection.SendRequestHelper(DirectoryRequest request, Int32& messageID)
at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout)
at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties)
--- End of inner exception stack trace ---
at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties& properties)
at System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval()
at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options, String userName, String password)我们知道这不是LDAP服务器本身,因为通过所讨论的方法,这里连接是没有错误的。我不太习惯在逻辑流中使用try...catch,我也读过关于这个方法的其他一些问题(比如不适当地尊重证书等等),所以我尝试用PrincipalContext来实现这一点。
有人能给我一些指导吗?我在这件事上要疯了。
编辑:经过一些进一步的研究,看起来这可能是自签名证书的一个问题。
编辑2:将其分解为X509链调用后,我得到了以下特定错误:
PartialChain无法将证书链构建到受信任的根授权机构。
考虑到这一点,您可能会认为这只是将CA作为受信任的根添加的问题,但这似乎并没有解决问题。
发布于 2017-10-24 03:23:35
我想知道这个CodeProject文章是否有帮助:
“最近,我在使用System.DirectoryServices.DirectoryEntry连接到Novell eDirectory服务器时遇到了麻烦,因为证书是自签名的。当在ASP.NET应用程序中运行时,没有检查机器级证书存储区。因此,即使自签名证书位于可信存储区,DirectoryEntry仍然拒绝建立连接。”
对于这种情况,LdapConnection类是一个更好的选择,因为它允许用户手动验证证书。注意,在Windows应用程序中运行时,DirectoryEntry方法可以很好地处理受信任的自签名证书。“
使用LdapConnection,您可以使用您自己的证书身份验证,如下所示:
LdapConnection con = new LdapConnection(new LdapDirectoryIdentifier("EDIRECTORYSERVER:636"));
con.SessionOptions.SecureSocketLayer = true;
con.SessionOptions.VerifyServerCertificate =
new VerifyServerCertificateCallback(ServerCallback);
con.Credential = new NetworkCredential(String.Empty, String.Empty);
con.AuthType = AuthType.Basic;还有这样的验证回传:
public static bool ServerCallback(LdapConnection connection, X509Certificate certificate)
{
try
{
X509Certificate expectedCert =
X509Certificate.CreateFromCertFile("C:\\certificates\\certificate.cer");
if (expectedCert.Equals(certificate))
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}发布于 2017-10-20 16:07:54
检查加布里埃尔·卢西的这个答案
您确定它支持SSL并且防火墙是打开的以允许连接吗?
LDAP使用端口389。LDAPS使用端口636。
如果安装了telnet客户端,可以使用它检查连接性:
telnet yourdomain.com 636如果你有一个空白的屏幕,它可以工作。如果它不能连接,它就会告诉你。
如果这是打开的,但仍然不起作用,它可能正在使用自签名SSL证书。检查Windows事件日志中与证书相关的错误。
我还用Chrome来检查证书。你必须像这样运行chrome:
chrome.exe --显式允许--端口=636,然后浏览到https://yourdomain.com:636,看看它是否给出了任何证书错误。然后你就可以看到证书了。如果这是问题所在,您可以导入证书并显式地信任它。
https://stackoverflow.com/questions/46738485
复制相似问题