【问题标题】:LdapConnection Bind TimeoutLdapConnection 绑定超时
【发布时间】:2021-09-23 21:07:39
【问题描述】:

有没有办法使用 .NET 附带的 System.DirectoryServices.Protocols.LdapConnection 在 LDAP 连接上设置 bind 超时?不要与 connection 超时(即Timeout 属性)相混淆。本质上,我需要按照here 的描述设置LDAP_OPT_TIMELIMIT

LdapSessionOptions 似乎是这样的地方,但据我所知,这个特定选项不存在。我还缺少什么吗?

【问题讨论】:

  • 在.net core中寻找类似的问题

标签: c# .net ldap


【解决方案1】:

这是我想出的解决方案:

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 库无论如何都在使用它,所以我觉得这没什么大不了的。

【讨论】:

  • 这是我一直在寻找的,特别是针对在 .Bind 调用期间发生并且需要超过默认 30 秒的 MFA 场景。
  • 太棒了,希望对您有所帮助。这正是我正在处理的问题。
  • 这是一个很好的解决方案,但仅适用于 .net 框架(特定于 Windows)。在 .net 核心中寻找相同的东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
  • 2013-10-13
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多