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.