Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to Active Directory via LDAP

I want to connect to our local Active Directory with C#.

I've found this good documentation.

But I really don't get how to connect via LDAP.

Can somebody of you explain how to use the asked parameters?

Sample Code:

  static DirectoryEntry createDirectoryEntry()     {        // create and return new LDAP connection with desired settings         DirectoryEntry ldapConnection     = new DirectoryEntry("rizzo.leeds-art.ac.uk");        ldapConnection.Path               = "LDAP://OU=staffusers,DC=leeds-art,DC=ac,DC=uk";        ldapConnection.AuthenticationType = AuthenticationTypes.Secure;        return ldapConnection;     }   

I just have the Hostname and the IP Address of our Active Directory Server. What does DC=xxx,DC=xx and so on mean?

like image 248
Waren Schild Avatar asked Feb 11 '13 13:02

Waren Schild


People also ask

How does LDAP connect to Active Directory?

How does LDAP work with Active Directory? LDAP provides a means to manage user and group membership stored in Active Directory. LDAP is a protocol to authenticate and authorize granular access to IT resources, while Active Directory is a database of user and group information.

Can Active Directory work with LDAP?

Active Directory (AD) supports both Kerberos and LDAP – Microsoft AD is by far the most common directory services system in use today. AD provides Single-SignOn (SSO) and works well in the office and over VPN.


2 Answers

DC is your domain. If you want to connect to the domain example.com than your dc's are: DC=example,DC=com

You actually don't need any hostname or ip address of your domain controller (There could be plenty of them).

Just imagine that you're connecting to the domain itself. So for connecting to the domain example.com you can simply write

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com"); 

And you're done.

You can also specify a user and a password used to connect:

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com", "username", "password"); 

Also be sure to always write LDAP in upper case. I had some trouble and strange exceptions until I read somewhere that I should try to write it in upper case and that solved my problems.

The directoryEntry.Path Property allows you to dive deeper into your domain. So if you want to search a user in a specific OU (Organizational Unit) you can set it there.

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com"); directoryEntry.Path = "LDAP://OU=Specific Users,OU=All Users,OU=Users,DC=example,DC=com"; 

This would match the following AD hierarchy:

  • com
    • example
      • Users
        • All Users
          • Specific Users

Simply write the hierarchy from deepest to highest.

Now you can do plenty of things

For example search a user by account name and get the user's surname:

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com"); DirectorySearcher searcher = new DirectorySearcher(directoryEntry) {     PageSize = int.MaxValue,     Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=AnAccountName))" };  searcher.PropertiesToLoad.Add("sn");  var result = searcher.FindOne();  if (result == null) {     return; // Or whatever you need to do in this case }  string surname;  if (result.Properties.Contains("sn")) {     surname = result.Properties["sn"][0].ToString(); } 
like image 194
The-First-Tiger Avatar answered Oct 05 '22 23:10

The-First-Tiger


ldapConnection is the server adres: ldap.example.com Ldap.Connection.Path is the path inside the ADS that you like to use insert in LDAP format.

OU=Your_OU,OU=other_ou,dc=example,dc=com

You start at the deepest OU working back to the root of the AD, then add dc=X for every domain section until you have everything including the top level domain

Now i miss a parameter to authenticate, this works the same as the path for the username

CN=username,OU=users,DC=example,DC=com

Introduction to LDAP

like image 34
s.lenders Avatar answered Oct 06 '22 01:10

s.lenders