.net 核心中的 LDAPConnection 绑定超时
LDAPConnection Bind timeout in .net core
我无法在 .net 核心中找到有关 LdapConnection 绑定超时的任何信息。一种解决方案是 但它在 .net 核心中不起作用。
LdapConnection 超时和绑定超时似乎是不同的东西。
我的代码如下
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);
我试过连接超时,但看起来没有效果。
我收到异常消息 ExceptionCode: 81,Message: LDAP 服务器在随机时间间隔内不可用。有时 18 秒,有时超过一分钟。
我正在通过 vpn 连接的帮助重现问题。我的本地 ldap 通过首先连接到某个 vpn 进行连接。
使用 vpn 连接一切正常。但是如果没有 vpn,我对 Bind() 方法的调用将无限期地等待
可以使用 Task 设置超时,一旦达到以秒为单位的超时,就会发生取消事件以退出此事件,如下所示
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);
}
我无法在 .net 核心中找到有关 LdapConnection 绑定超时的任何信息。一种解决方案是
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);
我试过连接超时,但看起来没有效果。 我收到异常消息 ExceptionCode: 81,Message: LDAP 服务器在随机时间间隔内不可用。有时 18 秒,有时超过一分钟。 我正在通过 vpn 连接的帮助重现问题。我的本地 ldap 通过首先连接到某个 vpn 进行连接。 使用 vpn 连接一切正常。但是如果没有 vpn,我对 Bind() 方法的调用将无限期地等待
可以使用 Task 设置超时,一旦达到以秒为单位的超时,就会发生取消事件以退出此事件,如下所示
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);
}