Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to LDAP from C# using DirectoryServices

I am trying to connect to an edirectory 8.8 server running LDAP. How would I go about doing that in .Net? Can I still use the classes in System.DirectoryService such as DirectoryEntry and DirectorySearcher or are they AD specific? Do I need to specify the "Connection String" any differently?

I am trying something like the code below but it doesn't seem to work...

DirectoryEntry de = new DirectoryEntry ("LDAP://novellBox.sample.com","admin","password",AuthenticationTypes.None);
DirectorySearcher ds = new DirectorySearcher(de);
var test = ds.FindAll();

Any ideas?

like image 224
Chaitanya Avatar asked Sep 17 '09 07:09

Chaitanya


2 Answers

Well, I think your connection string is missing a bit - specifying just the server name isn't good enough - you also need to specify a "starting point" for your search.

In AD, this would typically be something like the "Users" container in your domain, which you'd specify like this in LDAP parlance:

LDAP://novellBox.sample.com/cn=Users,dc=YourCompany,dc=com

Not sure how LDAP compliant the newer versions of eDirectory are - but that should work since in theory, it's standard LDAP regardless of the implementation :-)

But then again: only in theory, there's no difference between theory and practice.....

There's also a System.DirectoryServices.Protocols namespace which offers low-level LDAP calls directly - and that's definitely not tied to AD at all, but it's really quite low-level.....

There's also a Novell C# LDAP library but I've never tried it and can't say how complete or capable it is. It might give you some clues, though!

Also see this other Stackoverflow question about Novell, LDAP and C# - it might give you additional info.

like image 117
marc_s Avatar answered Oct 19 '22 07:10

marc_s


I had a hard time figuring this out but you could use something like the following, it worked sweet for me:

Domain domain = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, "novellBox.sample.com");
DirectorySearcher ds = new DirectorySearcher(domain.GetDirectoryEntry(), searchQuery);
using (SearchResultCollection src = ds.FindAll())
{....}
like image 6
Fermin Avatar answered Oct 19 '22 07:10

Fermin