简体   繁体   中英

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

Thanks for your help.

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

Even though I solved my problem I want to share with other developers what I achieved so far. 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 him 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:
- Registering change notification with Active Directory using C#

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 no IP address of remote server is needed, just Domain Controller that is used on him, and that Domain used for logging is unnecessary.

Happy coding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM