How to know if my DirectoryEntry is really connected to my LDAP directory?

前端 未结 4 605
猫巷女王i
猫巷女王i 2020-12-20 16:48

I\'m connecting to a LDAP directory in C#, so I\'ve used the DirectoryEntry class.

When you do the \"new DirectoryEntry\" with address, login, and password it is sup

相关标签:
4条回答
  • 2020-12-20 17:35

    Ok so marc_s's solution was approximately what i was doing (except i was looking for SchemaEntry and not NativeObject). But the timeout delay is much too long (the query is run to fill autocompletion values for a form). I think I actually prefer to pretend the connection is open and let the query run. That way, i can set my own, smaller, timeout delay.

    0 讨论(0)
  • 2020-12-20 17:36

    You can check DirectoryEntry.Properties.Count. If it's > 0, it's a valid object. .Properties is never null - you'll be able to read the count even if you're not connected up to a valid DirectoryEntry, and a valid DE will always have at least one property.

    No try/catch or exceptions necessary.

    0 讨论(0)
  • 2020-12-20 17:45

    You can check DirectoryEntry.Properties.Count. If it's > 0,for a valid object. But still let say your LDAP server is down. you can't identify it with any of its properties.Instead you can catch it using the try catch block

    try         
    {    
         entry = new DirectoryEntry("priorityLDAPServer", sUserName, sPassword, AuthenticationTypes.None);      
       if(entry.Properties.Count > 0) 
       {               
            object o = entry.NativeObject;        
         `   next need to check user record in application database`      
       }
     }        
        catch (System.Runtime.InteropServices.COMException comex)       
    {    
    
     //throws you the error if LDAP   server is down or wrong "Server is invalid "          
     //  you can further do a nested try catch within this block if you to try a     optional LDAP server.*
    }       
    

    Hope this helps you

    0 讨论(0)
  • 2020-12-20 17:50

    Just "newing" up a DirectoryEntry does NOT create a connection to the LDAP store.

    Only once you start using its properties, or when you access the .NativeObject property explicitly, you'll actually get a connection to the LDAP store.

    In order to make sure you're connected, just read out the (DirectoryEntry).NativeObject in a try...catch clause - if it bombs out, you have a problem, otherwise your connection is now up and active.

    Unfortunately, to my knowledge, there is no property or method you can call to figure out whether or not you've successfully connected to LDAP using DirectoryEntry.

    Marc

    0 讨论(0)
提交回复
热议问题