Before you waste your time with code, confirm that the LDAP server to which you are trying to connect is available.
Normally one connects to an LDAP server on port tcp/389, or LDAPs on tcp/636. Poprt 445 is not required for LDAP queries. It may be required for some native Active Directory connections.
Start by trying to the server at the command line.
telnet ldap.example.com 389
If Telnet can't establish a connection, you know why your code can't.
If you are able to connect, download and try a free LDAP browser tool.
If both of the above work, it's your code's fault.
Here's a snippet of code from the last time I used LDAP authentication:
public bool Authenticate(string UserID, string Password)
{
var L1 = ActivityLog.AddAsync("Authenticating user '{0}'", UserID);
if (UserID == string.Empty || Password == string.Empty)
{
L1.Fail(new Exception("UserID and Password cannot be empty."));
return false;
}
var credential = new NetworkCredential(UserID, Password, CoreSettings.Current.ActiveDirectoryDomain);
var server = CoreSettings.Current.LDAPServer;
var ips = Dns.GetHostAddresses(server);
foreach (var ip in ips)
{
var L2 = ActivityLog.AddAsync(Importances.Debug, "Connecting to LDAP server '{0} ({1})'", server, ip);
using (var ldap = new LdapConnection(server))
{
try
{
ldap.Timeout = TimeSpan.FromSeconds(LdapTimeout);
ldap.Bind(credential);
L2.End();
L1.End();
return true;
}
catch (Exception ex)
{
L2.Fail(ex);
throw;
}
}
}
return false;
}