首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LdapConnection绑定超时

LdapConnection绑定超时
EN

Stack Overflow用户
提问于 2021-07-14 22:13:51
回答 1查看 341关注 0票数 1

是否有一种方法可以使用附带的System.DirectoryServices.Protocols.LdapConnection在.NET连接上设置System.DirectoryServices.Protocols.LdapConnection绑定.NET超时?不要与连接超时(即超时属性)混淆。本质上,我需要按照描述的LDAP_OPT_TIMELIMIT设置这里

LdapSessionOptions似乎是实现这一目标的地方,但正如我所看到的,这个特定的选项并不存在。还有什么东西我遗漏了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-01 15:18:50

以下是我想出的解决方案:

代码语言:javascript
复制
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,不适合跨平台使用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68385657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档