Reputation: 4124
I am unable to found anything for LdapConnection Bind timeout in .net core. one solution is LdapConnection Bind Timeout but it is not working in .net core. LdapConnection timeout and bind timeout seems to be different thing. my code is something like following
var credentials = new NetworkCredential(obj.AdminBindUserName, obj.AdminBindPassword);
if (obj.LDAPAddressPort.HasValue && obj.LDAPAddressPort > 0)
{
connection = new LdapConnection(new LdapDirectoryIdentifier(obj.LDAPAddress, obj.LDAPAddressPort.Value, false, false));
//connection = new LdapConnection(new LdapDirectoryIdentifier(obj.LDAPAddress, obj.LDAPAddressPort));connection = new LdapConnection(new LdapDirectoryIdentifier(obj.LDAPAddress, obj.LDAPAddressPort));
}
else
{
connection = new LdapConnection(obj.LDAPAddress);
}
//connection.Timeout = new TimeSpan(0,0,5);
connection.SessionOptions.ProtocolVersion = 3;
connection.AuthType = AuthType.Basic;
connection.Bind(credentials);
I have tried connection timeout but looks like no effect of it. I am getting exception message ExceptionCode: 81,Message: The LDAP server is unavailable at random time interval. sometime 18 seconds, sometime more then a minute. I am reproducing the problem via help of vpn connectivity. my local ldap is connected via first connecting to some vpn. with vpn connection every thing works fine. but without vpn my call to Bind() method wait indefinitely
Upvotes: 1
Views: 866
Reputation: 2485
A timeout can be set using Task and once that timeout in second is reached a cancellation event will occur to get out of this as show below
if (obj.IsBindTimeLimit)
{
int timeoutSecond = obj.BindTimeOutInSeconds;
if (timeoutSecond <= 0)
{
timeoutSecond = 5;
}
CancellationToken tscancel = new CancellationToken();
Task t = Task.Run(() =>
{
var credentials = new NetworkCredential(obj.AdminBindUserName, obj.AdminBindPassword);
if (obj.LDAPAddressPort.HasValue && obj.LDAPAddressPort > 0)
{
connection = new LdapConnection(new LdapDirectoryIdentifier(obj.LDAPAddress, obj.LDAPAddressPort.Value, false, false));
}
else
{
connection = new LdapConnection(obj.LDAPAddress);
}
connection.SessionOptions.ProtocolVersion = 3;
connection.AuthType = AuthType.Basic;
connection.Bind(credentials);
});
if (!t.Wait(timeoutSecond * 1000, tscancel))
{
throw new TimeoutException("The timeout interval elapsed");
}
}
else
{
var credentials = new NetworkCredential(obj.AdminBindUserName, obj.AdminBindPassword);
if (obj.LDAPAddressPort.HasValue && obj.LDAPAddressPort > 0)
{
connection = new LdapConnection(new LdapDirectoryIdentifier(obj.LDAPAddress, obj.LDAPAddressPort.Value, false, false));
}
else
{
connection = new LdapConnection(obj.LDAPAddress);
}
connection.SessionOptions.ProtocolVersion = 3;
connection.AuthType = AuthType.Basic;
connection.Bind(credentials);
}
Upvotes: 2