Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!