Skip to content

System.DirectoryServices.Protocols (a.k.a. LDAP is your friend)

I’ve had the displeasure of trying to do some work with LDAP in .NET lately.  The interfaces are all there but the documentation and error handling isn’t.  (Read: The lights are on but no one is home.)  Because of this I was pleased to find there was a book out on the subject The .NET Developers Guide to Directory Services by Joe Kaplan and Ryan Dunn.

I also had the pleasure of sharing an email conversation with Joe.  I initiated a conversation with him slightly before I was able to pickup the book – because of his activity in the newsgroups.  I quickly figured out I needed to pick up the book and did.  I highly recommend it because the information that is contained in it is both unique — because it doesn’t exist elsewhere — and is easy to understand.  LDAP may be common but it’s not something that most developers do every day.  Back to my point, there were some interesting things that came out of that conversation.  I wanted to highlight them in part to document them and in part to thank Joe for his help.

1) System.DirectoryServices vs. System.DirectoryServices.Protocols – What I was trying to do was to develop a high performance LDAP ASP.NET 2.0 Membership Provider for a project.  In order to do that, I was going to be using LDAP to authenticate a large number of users.  One of the issues with System.DirectoryServices is cleaning up all of the connections quick enough in a high load environment.  The net of the conversation with Joe – which I agreed with wholeheartedly as I began to understand the underlying characteristics of System.DirectoryServices – was if you need to control cleanup of connections use System.DirectoryServices.Protocols.  The end I came to was that I think of System.DirectoryServices as a façade over System.DirectoryServices.Protocols.  Façade is a pattern that makes using something easier.

2) SSL is not your friend – There are lots of things to think about when trying to use SSL to encrypt your LDAP communications.  First, if you are using clients all in a domain, add an Enterprise Certification Authority to the domain.  If you do that the domain controllers will enroll themselves and start accepting SSL connections for LDAP – that’s cool.  If you don’t have all internal clients, you can refer to this KB article to add a third party certificate.  Even with it working (and tested via LDP) you may have a problem when you try to set SecureSocketLayer support.  You may receive a less than helpful DirectoryOperationException(“The server cannot handle directory requests.”) what isn’t quite so amusing about this is that it didn’t even try to communicate with the server.  The solution was to add the port number to the server.  So instead of passing “Server” to open the LdapConnection, I passed “server:636”.  By the way, LDAPS is port 636 – rather than the 389 port used by LDAP.

3) Fast Concurrent Bind isn’t really a bind – In LDAP you bind to associate credentials to your connection so that it can use those credentials to validate your operations.  One of the things that S.DS.P supports is the idea of a Fast Concurrent Bind.  Basically, check my credentials to make sure that they’re OK – and don’t do anything else.  I tried using Fast Concurrent Bind (because it is the fastest way to validate a user) to bind to a connection and then do a search.  So I did my bind and then did my search.  When I tried the search I got an exception – DirectoryOperationException(“An operation error occurred”) when I dug into the network capture I found a deeper message: “LdapErr: DSID-0C090627, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, vece”  It was funny because message 5 was a bind with a success response and message ID 6 – the one I got the error on was a search.  The issue at the end of the day I was I was using fast concurrent bind.  So I wasn’t really doing a full bind at all.  I was just authenticating the user.

With Joe’s help I was able to figure out what the issues were … and create a working solution.

3 Comments

  1. Hi Robert,

    Thank you so much for sharing this information. I was having trouble to decide which one to use (betweem DS and DS.P). Now I have decided to use DS.P.

  2. At the risk of being pedantic, I would think of System.DirectoryServices as a façade over the ADSI COM interface while System.DirectoryServices.Protocols is a
    façade over the native wldap32.dll interface. Of course ADSI uses wldap32.dll as well so I guess they both abstract the same library. Personally, I prefer S.DS.P as it seems a bit more intuitive to someone not well versed with ADSI.

    You are right about how bad these libraries are documented. Plus S.DS has brutal error messages/exceptions that it returns (I think everything is a ComException).

  3. Hey Robert,

    I am using the Async search using S.DS.P as described in the “The .NET Developers Guide to Directory Services” and keep getting the message “000020B9: SvcErr: DSID-0311045B, problem 5003 (WILL_NOT_PERFORM), data 0” from the server when it runs the code:
    SearchResponse response =
    (SearchResponse)connection.EndSendRequest(asyncResult);

    Is the server refusing the execute the search?


Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this: