public bool TryLoadLDAPUsers()
        {
            try
            {
                if (!Settings.EnableLdapAuthentication)
                {
                    return(false);
                }

                if (!LdapHelper.IsConnected)
                {
                    LdapHelper.Connect();
                }

                var users = LdapHelper.GetUsers();

                foreach (var user in users)
                {
                    if (string.IsNullOrEmpty(user.Sid))
                    {
                        AllSkipedDomainUsers.Add(user, LdapSettingsStatus.WrongSidAttribute);
                        continue;
                    }

                    if (!CheckLoginAttribute(user, Settings.LoginAttribute))
                    {
                        AllSkipedDomainUsers.Add(user, LdapSettingsStatus.WrongLoginAttribute);
                        continue;
                    }

                    if (!Settings.GroupMembership)
                    {
                        AllDomainUsers.Add(user);
                        continue;
                    }

                    if (!Settings.UserAttribute.Equals(LdapConstants.RfcLDAPAttributes.DN,
                                                       StringComparison.InvariantCultureIgnoreCase) && !CheckUserAttribute(user, Settings.UserAttribute))
                    {
                        AllSkipedDomainUsers.Add(user, LdapSettingsStatus.WrongUserAttribute);
                        continue;
                    }

                    AllDomainUsers.Add(user);
                }

                if (AllDomainUsers.Any())
                {
                    PrimaryGroupId = AllDomainUsers.First().GetValue(LdapConstants.ADSchemaAttributes.PRIMARY_GROUP_ID) as string;
                }

                return(AllDomainUsers.Any() || !users.Any());
            }
            catch (ArgumentException)
            {
                _log.ErrorFormat("TryLoadLDAPUsers(): Incorrect filter. userFilter = {0}", Settings.UserFilter);
            }

            return(false);
        }
        private string LoadLDAPDomain()
        {
            try
            {
                if (!Settings.EnableLdapAuthentication)
                {
                    return(null);
                }

                if (!LdapHelper.IsConnected)
                {
                    LdapHelper.Connect();
                }

                string ldapDomain;

                if (AllDomainUsers.Any())
                {
                    ldapDomain = AllDomainUsers.First().GetDomainFromDn();

                    if (!string.IsNullOrEmpty(ldapDomain))
                    {
                        return(ldapDomain);
                    }
                }

                ldapDomain = LdapHelper.SearchDomain();

                if (!string.IsNullOrEmpty(ldapDomain))
                {
                    return(ldapDomain);
                }

                ldapDomain = LdapUtils.DistinguishedNameToDomain(Settings.UserDN);

                if (!string.IsNullOrEmpty(ldapDomain))
                {
                    return(ldapDomain);
                }

                ldapDomain = LdapUtils.DistinguishedNameToDomain(Settings.GroupDN);

                if (!string.IsNullOrEmpty(ldapDomain))
                {
                    return(ldapDomain);
                }
            }
            catch (Exception ex)
            {
                _log.ErrorFormat("LoadLDAPDomain(): Error: {0}", ex);
            }

            return(null);
        }