LdapConnection 绑定超时
LdapConnection Bind Timeout
有没有办法使用 .NET 附带的 System.DirectoryServices.Protocols.LdapConnection
在 LDAP 连接上设置 bind 超时?不要与 connection 超时混淆(即 Timeout property). Essentially, I need to set the LDAP_OPT_TIMELIMIT
as described here.
LdapSessionOptions 似乎是那个地方,但据我所知,这个特定选项不存在。还有什么我想念的吗?
这是我想出的解决方案:
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-only,不适合跨平台。
有没有办法使用 .NET 附带的 System.DirectoryServices.Protocols.LdapConnection
在 LDAP 连接上设置 bind 超时?不要与 connection 超时混淆(即 Timeout property). Essentially, I need to set the LDAP_OPT_TIMELIMIT
as described here.
LdapSessionOptions 似乎是那个地方,但据我所知,这个特定选项不存在。还有什么我想念的吗?
这是我想出的解决方案:
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-only,不适合跨平台。