Created
October 9, 2023 03:51
-
-
Save muhadmr/45578cfd246b62b6271e86e82e098d35 to your computer and use it in GitHub Desktop.
Connect to ApacheDS using C# and Novell Directory Ldap library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // HOWTO | |
| // 1. Bring up docker, get the image xaked/apacheds:latest | |
| // 2. Make sure to expose the port, here i use 10389 | |
| // 3. Open Apache Directory Studio, try connect to the server. use username = uid=admin,ou=system, password = secret (from xaked/apacheds) | |
| // 4. If can connect, create a few entries. see http://krams915.blogspot.com/2011/01/ldap-apache-directory-studio-basic.html as example | |
| // 5. look at the code, below. look at documentation https://www.novell.com/documentation/developer/ldapcsharp/?page=/documentation/developer/ldapcsharp/cnet/data/front.html | |
| using Novell.Directory.Ldap; | |
| LdapConnection connection = new LdapConnection(); | |
| try | |
| { | |
| Console.WriteLine("Connecting to localhost:10389"); | |
| // Set the server address and port | |
| connection.Connect("localhost", 10389); // Use 636 for LDAPS | |
| // Authenticate with the server | |
| connection.Bind("uid=admin,ou=system", "secret"); | |
| // see documentation in item 5 | |
| string searchBase = "o=testing"; | |
| int searchScope = LdapConnection.ScopeSub; | |
| string searchFilter = "(o=testing)"; | |
| var lsc = connection.Search(searchBase, searchScope, "objectClass=*", null, false); | |
| while (lsc.HasMore()) | |
| { | |
| LdapEntry nextEntry = null; | |
| try | |
| { | |
| nextEntry = lsc.Next(); | |
| } | |
| catch (LdapException e) | |
| { | |
| Console.WriteLine("Error: " + e.LdapErrorMessage); | |
| //Exception is thrown, go for next entry | |
| continue; | |
| } | |
| Console.WriteLine("\n" + nextEntry.Dn); | |
| // Get the attribute set of the entry | |
| LdapAttributeSet attributeSet = nextEntry.GetAttributeSet(); | |
| System.Collections.IEnumerator ienum = attributeSet.GetEnumerator(); | |
| // Parse through the attribute set to get the attributes and the corresponding values | |
| while (ienum.MoveNext()) | |
| { | |
| LdapAttribute attribute = (LdapAttribute)ienum.Current; | |
| string attributeName = attribute.Name; | |
| string attributeVal = attribute.StringValue; | |
| Console.WriteLine(attributeName + "value:" + attributeVal); | |
| } | |
| } | |
| } | |
| catch (LdapException ex) | |
| { | |
| Console.Write("{0} {1}", ex.Message, ex.InnerException?.Message); | |
| // Handle LDAP exceptions... | |
| } | |
| finally | |
| { | |
| // Close the connection | |
| connection.Disconnect(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment