Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

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

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 supposed to connect to the LDAP directory.

However, even if the connection didn't work, it returns without problem, and the directoryentry variable is set.

So i do i know my connection is really opened ? Right now, I'm using a very very ugly hack : i put a "if(mydirectory.SchemaEntry)" which generates an exception if the connection wasn't etablished, because some of the members of the DirectoryEntry, such as SchemaEntry, aren't set if the connection failed. But 1:that's gotta be 11/10 on the ugliness scale 2:that takes a lot of time before failing.

So what is the good way to do this ? Surely, Microsoft must have provided something (even if I'm using a LDAP directory and not an Active Directory) to know if I'm really connected ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...