Ldap Connection pool, with windows service, 并重用相同的连接?

问题描述 投票:0回答:1

系统创建一个连接池来连接和管理到活动目录的连接。 目标是只创建和重复使用一个连接到已配置的广告,而不是为每个请求创建一个新连接。 问题截图: Screenshot of the problem:

事情是,在代码实现之后,在 VStudio 上运行代码更改成功应用,如下所示: Screenshot of the solution:

但是在我的机器上运行管道部署服务后,问题仍然存在,好像没有检测到代码的变化。它显示为初始问题的屏幕截图。

这是我第一次使用 Windows 服务,所以任何输入都会很棒。

试过这些,它按预期工作: 如何在 System.DirectoryServices 中保留跨调用的连接凭据?

所以我的代码看起来像这样,它在一个地方创建连接,而在这些 directoryEntry 调用之前都是围绕代码......(50 多个不同的地方)

public async Task<DirectoryEntry> GetDirectoryEntry(string path, string username ="", string password = "", AuthenticationTypes authType = 0)
{
    try
    {
        var ldapMembershipProvider = GetCurrentLdapProvider(path);

        if (ldapMembershipProvider.UseSSL == true)
        {
            if (ldapMembershipProvider.SearchScope != "" && ldapMembershipProvider.SearchScope != null)
            {
                var conn = new DirectoryEntry(path + "/" + ldapMembershipProvider.SearchScope, username , password, AuthenticationTypes.SecureSocketsLayer);
                return conn;
            }
            else
            {
                var conn = new DirectoryEntry(path, username, password, AuthenticationTypes.SecureSocketsLayer);
                return conn;
            }
        }
        else
        {
            if (ldapMembershipProvider.SearchScope != "" && ldapMembershipProvider.SearchScope != null)
            {
                var conn = new DirectoryEntry(path + "/" + ldapMembershipProvider.SearchScope, username + "@" + ldapMembershipProvider.ShortName, password);
                return conn;
            }
            else
            {
                var conn =  new DirectoryEntry(path, username + "@" + ldapMembershipProvider.ShortName, password);
                return conn;
            }
        }
    }
    catch (Exception ex)
    {
        await _loggingService.LogException("LdapConnectionService()", ex);
        throw (ex);
    }
}

但是带有代码的 Windows 服务没有显示所需的更改。

c# active-directory ldap windows-services
1个回答
0
投票

DirectoryEntry
包装 Windows 本机 ADSI(Active Directory 服务接口)。有关 ADSI 如何处理连接缓存的详细信息,请参见:Connection Caching.

这是要注意的重要部分:

如果在后续连接中使用相同的服务器、端口和凭据,并且只有 ADS_FAST_BIND 或 ADS_SERVER_BIND 身份验证标志不同,ADSI 将重用现有连接。

您编写的方法允许调用者传递不同的服务器、端口、凭据和身份验证类型。事实上,在您的屏幕截图中,它至少显示了不同的端口和身份验证类型:正在使用 389 和 636,这意味着 LDAP 和 LDAPS。如果您也使用不同的凭据,那么这将解释多个连接。

您还需要至少一个非处置对象来保持连接有效。因此,如果您所有的

DirectoryEntry
对象都被手动处理,或者在超出范围后被垃圾收集,那么您创建的下一个对象将打开一个新连接。

© www.soinside.com 2019 - 2024. All rights reserved.