Search code examples
c#.netactive-directoryldapldap-query

Connect to Active Directory using LdapConnection class on remote server


I have a problem: I need to connect from a remote server to Active Directory, but the code has to be using the LdapConnection class. I need this because that way I can only test change notifiers when some event happen (such as user is deactivated or he changed group, data etc). OS on the remote server is Windows Server 2012.

I managed to do this from local using DirectoryServices with the following code:

String ldapPath = "LDAP://XRMSERVER02.a24xrmdomain.info";
directoryEntry = new DirectoryEntry(ldapPath, @"A24XRMDOMAIN\username", "pass");

//// Search AD to see if the user already exists.
DirectorySearcher search = new DirectorySearcher(directoryEntry);
search.Filter = "(&(objectClass=user))";
SearchResult result = search.FindOne();

This is okay and connection works but now I need to connect using the LdapConnection class.

I tried something like this on many ways but none of that helped me:

LdapConnection connection = new LdapConnection(XRMSERVER02.a24xrmdomain.info);
var credentials = new NetworkCredential(@"A24XRMDOMAIN\username", "pass");             
connection.Credential = credentials;
connection.Bind();

It says that credentials are invalid but that is not true.

Explanations:

  • XRMSERVER02 - Domain controller
  • a24xrmdomain.info - Domain
  • A24XRMDOMAIN - Domain used for logging

Solution

  • Try using NetworkCredential constructor with 3 parameters: username, password and domain. Specify domain separately from user name