Mike Paxton
Mike Paxton

Reputation: 108

LdapConnection Bind Timeout

Is there a way to set the bind timeout on an LDAP connection using the System.DirectoryServices.Protocols.LdapConnection that comes with .NET? Not to be confused with the connection timeout (which is the Timeout property). Essentially, I need to set the LDAP_OPT_TIMELIMIT as described here.

LdapSessionOptions seems like the place for that, but near as I can see this particular option isn't present. Is there something else I'm missing?

Upvotes: 1

Views: 845

Answers (1)

Mike Paxton
Mike Paxton

Reputation: 108

Here's the solution I came up with:

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);
}

This works, but I certainly don't love all that reflection. Or the DllImport, although the .NET library uses that under the covers anyway, so I feel like that's not as big a deal.

Per the comments below, it's also good to note here that due to the dependency on Wldap32.dll, this would appear to be Windows-only and not suitable for cross-platform.

Upvotes: 3

Related Questions