Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. LDAP DirectoryEntry and Exceptions.

LDAP DirectoryEntry and Exceptions.

Scheduled Pinned Locked Moved C#
helpquestioncsharpc++wpf
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike Doner
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • M Mike Doner

      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

      H Offline
      H Offline
      Henry Minute
      wrote on last edited by
      #2

      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.”

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups