Dan
Dan

Reputation: 458

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:

Upvotes: 0

Views: 12167

Answers (2)

Dan
Dan

Reputation: 458

The problem that I encountered was that I had remote server with OS Windows Server 2012 and Active Directory on it. I needed to connect on it via my local machine (Windows 10).

As I stated in my question, it is possible to do that via DirectoryServices with the following code:

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

//// Test search on AD to see if connection works.
DirectorySearcher search = new DirectorySearcher(directoryEntry);
search.Filter = "(&(objectClass=user))";
SearchResult result = search.FindOne();

This is one of the solutions, but since my task was to get notification and to identify when ever some object has changed in Active Directory, I needed connection to Active Directory on Remote server via LDAP class. Code for getting notifiers is taken from:

I succeeded to connect with LDAP class via next code:

String ldapPath2 = "(DomainController).a24xrmdomain.info";
LdapConnection connection = new LdapConnection(ldapPath2);
var credentials = new NetworkCredential(@"username", "pass");             
connection.Credential = credentials;
connection.Bind();

Want to mention that the IP address of the remote server is bot needed, just the Domain Controller that is used on it, and that Domain used for logging is unnecessary.

Upvotes: 2

oldovets
oldovets

Reputation: 705

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

Upvotes: 1

Related Questions