tags:

views:

27

answers:

2

I am writing a .NET application to query an LDAP server, and I can't figure out how to look at exceptions and figure out what happened on the LDAP end. I'm using DirectoryServices, and trying to avoid anything that's specific to ActiveDirectory.

I create a DirectorySearcher, then do

try
{  
    SearchResult result = searcher.FindOne();
}
catch(Exception e)
{
    // now what?
}

I expect certain types of errors to occur on the LDAP end, like user not found, account disabled, etc., and I'd like to identify these particular errors. Are there specific expection types?

I notice the innerException has a _COMPlusExceptionCode. Is this a reliable indicator of what went wrong on the LDAP side? I haven't been able to find an enumeration of these exception codes.

Any suggestions?

A: 

Now call .ToString() and look at the human-readable text.

Steven Sudit
+1  A: 

There's a list of all the ADSI error codes here which contains generic COM and generic ADSI errors, as well as the specific LDAP error codes for ADSI 2.0.

LDAP errors are notoriously hard to deal with - more often than not, you just get a "server unable to perform" or something like that, without much explanation what causes the server to be unable to do its job.

marc_s
Which is why I suggest treating all errors as equally fatal and showing the explanation to the user.
Steven Sudit
@Steven Sudit: only that "explanation" (i.e. the Exception message provided by ADSI/Win32) usually doesn't have much meaning to the user, either :-(
marc_s
I do want to be able to separate out a few cases, to distinguish configuration problems from bad logins, and user not found from bad password. Thanks - this helps a lot!
Mike Kantor
@marc: You have a good point.
Steven Sudit