LDAP DirectoryEntry and Exceptions.
-
I'm doing some work with Active Directory using C# and the DirectoryEntry object. I'm fairly new to C# and exceptions, go easy on me! Some Code:
DirectoryEntry entry = new DirectoryEntry ( LDAPServer, domainAndUsername, Password ); try { // Bind to the native AdsObject to force authentication. Object obj = entry.NativeObject; } catch (Exception ex) { ErrorMessage = ex.Message; return false; }
Now, it seems that when binding to the DirectoryEntry fails, the exception is called. The error is returned as a string, which gives me an idea as to what happened, But how do I test what the error was programmatically? Is there some kind of numeric value as well in the exception that I can test? Basically, I want to know if the connection failed because of a bad ldapserver, or a username/password mismatch etc. I'm trying to create code that does a fail-over to a second ldap server if the primary ldap server is offline. I don't want to jump to the second server to retry the authentication, if there was an error with the credentials. Is there another exception I can test for here? How would I found out what other exceptions are available for this object? Any help you could give would be great.. Heck, I'll buy you a beer! Mike
-
I'm doing some work with Active Directory using C# and the DirectoryEntry object. I'm fairly new to C# and exceptions, go easy on me! Some Code:
DirectoryEntry entry = new DirectoryEntry ( LDAPServer, domainAndUsername, Password ); try { // Bind to the native AdsObject to force authentication. Object obj = entry.NativeObject; } catch (Exception ex) { ErrorMessage = ex.Message; return false; }
Now, it seems that when binding to the DirectoryEntry fails, the exception is called. The error is returned as a string, which gives me an idea as to what happened, But how do I test what the error was programmatically? Is there some kind of numeric value as well in the exception that I can test? Basically, I want to know if the connection failed because of a bad ldapserver, or a username/password mismatch etc. I'm trying to create code that does a fail-over to a second ldap server if the primary ldap server is offline. I don't want to jump to the second server to retry the authentication, if there was an error with the credentials. Is there another exception I can test for here? How would I found out what other exceptions are available for this object? Any help you could give would be great.. Heck, I'll buy you a beer! Mike
One way to do this is to have a series of
catch
blocks.try { // Bind to the native AdsObject to force authentication. Object obj = entry.NativeObject; } catch (System.UnauthorizedAccessException e) { MethodToDealWithThisTypeOfError(aParameterFromEventArgs, anotherParameter); return false; } catch (System.DivideByZeroError e) { MethodToDealWithThisTypeOfError(aParameterFromEventArgs, anotherParameter); return false; } finally { MeassageBox.Show("This happens regardless of errors", Application.ProductName); }
and so on. [Edit] Here is a reasonable article[^] [/Edit]
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”