Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using System.DirectoryServices to query active directory to authenticate/fetch users' info in a winforms appliation. Something like below:

var path = "LDAP://" + domain;
var entry = new DirectoryEntry(path);
DirectorySearcher myDirectorySearcher = new DirectorySearcher(entry);
var filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", username);
myDirectorySearcher.Filter = filter;  

I can only test this code on company's Active Directory. Is this going to work on any technology that supports LDAP?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

The System.DirectoryServices namespace is optimized for Active Directory. It will work against other LDAP servers - with certain limitations.

There's also the System.DirectoryServices.Protocols (see MSDN documentation and intro MSDN article) namespace (new in .NET 2.0) which is more of a low-level LDAP implementation - you need to do more work and write more code, but it's more portable and more likely to work with other LDAP stores.

There's also the System.DirectoryServices.AccountManagement (see MSDN documentation) namespace (new in .NET 3.5) which is a much nicer and simpler approach to using Active Directory from .NET - much improved over the S.DS stuff! But this is Active Directory only as far as I can tell.

share|improve this answer
    
Whata are the limitations of System.DirectoryServices for other LDAP servers? –  Kamyar Jan 2 '12 at 10:55
1  
@Kamyar: that totally depends on the other LDAP implementations - some vendors implement the standard with slight deviations (standards almost always leave some room for interpretation). It most notably won't work fully against e.g. Novell NDS (which claims to support LDAP as of NDS 7). Microsoft's S.DS implementation e.g. doesn't support the O=.... element in LDAP, if I remember correctly (which Novell NDS 7 uses). And some users have tried using S.DS against OpenLDAP and also found some inconsistencies/incompatibilities. Use Google or Bing to find those blog posts / articles! –  marc_s Jan 2 '12 at 10:57
    
Thanks. Would appreciate it if you take a look at stackoverflow.com/q/8700115/337294 too. –  Kamyar Jan 2 '12 at 11:11

You should change the filter to look like this:

var filter = string.Format("(&(objectCategory={0})(objectClass={1})(sAMAccountName={2}))", "person", "user", username);

This isn't going to generically work with any LDAP directory, though. sAMAccountName, for example, is an AD specific attribute.

share|improve this answer

The last time I tried to use system.directoryservices with a Novell network it just completely didn't work, exceptions were just thrown all over the place. Sorry I can't be more specific with version numbers.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.