private LdapConnection BindToLdap()

in src/Zyborg.PassCore.PasswordProvider.LDAP/LdapPasswordChangeProvider.cs [305:340]


        private LdapConnection BindToLdap()
        {
            var ldap = new LdapConnection();
            if (_ldapRemoteCertValidator != null)
                ldap.UserDefinedServerCertValidationDelegate += _ldapRemoteCertValidator;

            ldap.SecureSocketLayer = _options.LdapSecureSocketLayer;

            string? bindHostname = null;

            foreach (var h in _options.LdapHostnames)
            {
                try
                {
                    ldap.Connect(h, _options.LdapPort);
                    bindHostname = h;
                    break;
                }
                catch (Exception ex)
                {
                    _logger.LogWarning($"Failed to connect to host [{h}]", ex);
                }
            }

            if (string.IsNullOrEmpty(bindHostname))
            {
                throw new ApiErrorException("Failed to connect to any configured hostname", ApiErrorCode.InvalidCredentials);
            }

            if (_options.LdapStartTls)
                ldap.StartTls();

            ldap.Bind(_options.LdapUsername, _options.LdapPassword);

            return ldap;
        }