/// <summary>
        /// Initializes a new instance of the <see cref="LdapPasswordChangeProvider"/> class.
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <param name="options">The _options.</param>
        public LdapPasswordChangeProvider(ILogger logger, IOptions <LdapPasswordChangeOptions> options)
        {
            _logger  = logger;
            _options = options.Value;

            Init();
        }
Esempio n. 2
0
        private LdapConnection BindToLdap(LdapPasswordChangeOptions options)
        {
            var ldap = new LdapConnection();

            if (_ldapRemoteCertValidator != null)
            {
                ldap.UserDefinedServerCertValidationDelegate += _ldapRemoteCertValidator;
            }

            ldap.SecureSocketLayer = options.LdapStartTls;

            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 (ldap.SecureSocketLayer)
            {
                ldap.StartTls();
            }

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

            return(ldap);
        }