I have the following two implementations of authenticating users with LDAP and LDAPS and I was wondering which was better / more correct. For the record, both of these work on both SSL and non-SSL connections.
I'm also curious because when watching with Wireshark on the Non-SSL PrincipalContext
version, I still see traffic on Port 636. Of the four combinations (Non-SSL LdapConnection
, SSL LdapConnection
, Non-SSL PrincipalContext
, SSL PrincipalContext
) it is the only one that has traffic on both Port 389 and 636 instead of just one or the other. What could be causing this?
LDAP Connection Method:
bool userAuthenticated = false;
var domainName = DomainName;
if (useSSL)
{
domainName = domainName + ":636";
}
try
{
using (var ldap = new LdapConnection(domainName))
{
var networkCredential = new NetworkCredential(username, password, domainName);
ldap.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback((con, cer) => true);
ldap.SessionOptions.SecureSocketLayer = useSSL;
ldap.SessionOptions.ProtocolVersion = 3;
ldap.AuthType = AuthType.Negotiate;
ldap.Bind(networkCredential);
}
// If the bind succeeds, we have a valid user/pass.
userAuthenticated = true;
}
catch (LdapException ldapEx)
{
// Error Code 0x31 signifies invalid credentials, anything else will be caught outside.
if (!ldapEx.ErrorCode.Equals(0x31))
{
throw;
}
}
return userAuthenticated;
PrincipalContext Method:
bool userAuthenticated = false;
var domainName = DomainName;
if (useSSL)
{
domainName = domainName + ":636";
ContextOptions options = ContextOptions.SimpleBind | ContextOptions.SecureSocketLayer;
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName, null, options))
{
userAuthenticated = pc.ValidateCredentials(username, password, options);
}
}
else
{
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domainName))
{
userAuthenticated = pc.ValidateCredentials(username, password);
}
}
return userAuthenticated;
@DTI-Matt, in the examples above, you use
VerifyServerCertificate
callback that always returnstrue
. This, essentially, defies the purpose of connecting to LDAP over SSL, as no real certificate check is performed.While you could implement a real certificate check using
X509Chain
and/orX509Certificate2
classes, it seemsPrincipalContext
handles the checks for you.To summarize, both
LdapConnection
andPrincipalContext
provide very similar functionality, in means of connecting to an LDAP server over plain or SSL connection. You have to supplyLdapConnection
much more hand-written code for it to work properly. On the other hand,PrincipalContext
gives you the same functionality with less code to write by hand.As a note, connections to port 636 (your default LDAP over SSL port), by non-SSL
PrincipalContext
may be explained by the fact this class tries to connect as secure as possible.This is what we ended up with that is working over SSL/Non-SSL.