是否有一种方法可以使用附带的System.DirectoryServices.Protocols.LdapConnection在.NET连接上设置System.DirectoryServices.Protocols.LdapConnection绑定.NET超时?不要与连接超时(即超时属性)混淆。本质上,我需要按照描述的LDAP_OPT_TIMELIMIT设置这里。
LdapSessionOptions似乎是实现这一目标的地方,但正如我所看到的,这个特定的选项并不存在。还有什么东西我遗漏了吗?
发布于 2021-10-01 15:18:50
以下是我想出的解决方案:
private const int LDAP_OPT_TIMELIMIT = 0x04;
[DllImport("Wldap32.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "ldap_set_optionW", CharSet = CharSet.Unicode)]
private static extern int ldap_set_option([In] IntPtr handle, [In] int option, [In] ref int inValue);
private static void SetLdapConnectionBindTimeout(LdapConnection conn, int timeoutSeconds)
{
// We need the underlying LdapConnection handle; that's internal, so reflection here we go.
var handleField = typeof(LdapConnection).GetField("ldapHandle", BindingFlags.NonPublic | BindingFlags.Instance);
var handleWrapper = handleField.GetValue(conn);
// That handle object is itself a wrapper class around the IntPtr we actually need.
// The wrapper class is internal, and so is the IntPtr, so more reflection.
var internalHandleField = handleWrapper.GetType().GetField("handle", BindingFlags.NonPublic | BindingFlags.Instance);
var internalHandle = (IntPtr)internalHandleField.GetValue(handleWrapper);
// Now we can set.
ldap_set_option(internalHandle, LDAP_OPT_TIMELIMIT, ref timeoutSeconds);
}这是可行的,但我当然不喜欢所有的反射。或者DllImport,尽管.NET库无论如何都会在封面下使用它,所以我觉得这没什么大不了的。
根据下面的注释,这里也很好地注意到,由于对Wldap32.dll的依赖,这似乎仅限于Windows,不适合跨平台使用。
https://stackoverflow.com/questions/68385657
复制相似问题