C# 如何知道我的 DirectoryEntry 是否真的连接到我的 LDAP 目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1063642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to know if my DirectoryEntry is really connected to my LDAP directory?
提问by Ksempac
I'm connecting to a LDAP directory in C#, so I've used the DirectoryEntry class.
我正在连接到 C# 中的 LDAP 目录,因此我使用了 DirectoryEntry 类。
When you do the "new DirectoryEntry" with address, login, and password it is supposed to connect to the LDAP directory.
当您使用地址、登录名和密码执行“新 DirectoryEntry”时,它应该连接到 LDAP 目录。
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.
所以我知道我的连接真的打开了吗?现在,我正在使用一个非常非常丑陋的 hack :我放置了一个“if(mydirectory.SchemaEntry)”,如果没有建立连接,它会生成一个异常,因为 DirectoryEntry 的一些成员,例如 SchemaEntry,如果连接失败,则不设置。但是 1:这在丑陋的尺度上必须是 11/10 2:在失败之前需要很多时间。
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 ?
那么这样做的好方法是什么?当然,Microsoft 必须提供一些信息(即使我使用的是 LDAP 目录而不是 Active Directory)来了解我是否真的已连接?
采纳答案by Ksempac
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.
好的,marc_s 的解决方案与我正在做的差不多(除了我正在寻找 SchemaEntry 而不是 NativeObject)。但是超时延迟太长(运行查询以填充表单的自动完成值)。我想我实际上更喜欢假装连接是打开的并让查询运行。这样,我可以设置自己的、更小的超时延迟。
回答by marc_s
Just "newing" up a DirectoryEntry does NOTcreate a connection to the LDAP store.
只是“newing”上一个DirectoryEntry确实不创建到LDAP存储库的连接。
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.
只有在您开始使用它的属性后,或者当您.NativeObject
显式访问该属性时,您才会真正获得到 LDAP 存储的连接。
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.
为了确保您已连接,只需读出(DirectoryEntry).NativeObject
try...catch 子句中的 - 如果它爆炸了,则说明您有问题,否则您的连接现在已启动并处于活动状态。
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.
不幸的是,据我所知,您无法调用任何属性或方法来确定您是否已使用 DirectoryEntry 成功连接到 LDAP。
Marc
马克
回答by ScottBai
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.
您可以检查 DirectoryEntry.Properties.Count。如果它 > 0,则它是一个有效对象。.Properties 永远不会为空 - 即使您没有连接到有效的 DirectoryEntry,您也可以读取计数,并且有效的 DE 将始终至少具有一个属性。
No try/catch or exceptions necessary.
不需要 try/catch 或异常。
回答by Seeni Abirami
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
您可以检查 DirectoryEntry.Properties.Count。如果它 > 0,则为有效对象。但仍然假设您的 LDAP 服务器已关闭。你不能用它的任何属性来识别它。相反,你可以使用 try catch 块来捕捉它
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
希望这对你有帮助