private void btnBegin_Click(object sender, EventArgs e)
        {
            SplashForm.ShowSplashScreen();

            LDAPUserImporter li = new LDAPUserImporter();

            li.ImportLDAPUser();

            SplashForm.CloseForm();

            MessageBox.Show("Users Synced Successfully!");
        }
예제 #2
0
        public override byte CheckSettings(LDAPSupportSettings settings, LDAPUserImporter importer, bool acceptCertificate = false)
        {
            // call static constructor of MonitorEventRequest class
            MonitorEventRequest.RegisterResponseTypes = true;
            novellLdapHelper.AcceptCertificate        = acceptCertificate;
            if (!settings.EnableLdapAuthentication)
            {
                return(OPERATION_OK);
            }
            string password = GetPassword(settings.PasswordBytes);

            if (settings.Server.Equals("LDAP://", StringComparison.InvariantCultureIgnoreCase))
            {
                return(WRONG_SERVER_OR_PORT);
            }
            try
            {
                if (settings.Authentication)
                {
                    CheckCredentials(settings.Login, password, settings.Server, settings.PortNumber, settings.StartTls);
                }
            }
            catch (NovellLdapTlsCertificateRequestedException ex)
            {
                CertificateConfirmRequest = ex.CertificateConfirmRequest;
                return(CERTIFICATE_REQUEST);
            }
            catch (NotSupportedException)
            {
                return(TLS_NOT_SUPPORTED);
            }
            catch (InvalidOperationException)
            {
                return(CONNECT_ERROR);
            }
            catch (ArgumentException)
            {
                return(WRONG_SERVER_OR_PORT);
            }
            catch (SecurityException)
            {
                return(STRONG_AUTH_REQUIRED);
            }
            catch (SystemException)
            {
                return(WRONG_SERVER_OR_PORT);
            }
            catch (Exception)
            {
                return(CREDENTIALS_NOT_VALID);
            }
            if (!CheckUserDN(settings.UserDN, settings.Server, settings.PortNumber,
                             settings.Authentication, settings.Login, password, settings.StartTls))
            {
                return(WRONG_USER_DN);
            }
            try
            {
                importer.AllDomainUsers = novellLdapHelper.GetUsersByAttributes(settings);
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Incorrect filter. userFilter = {0}, {1}", settings.UserFilter, ex);
                return(INCORRECT_LDAP_FILTER);
            }
            if (importer.AllDomainUsers == null || importer.AllDomainUsers.Count == 0)
            {
                log.ErrorFormat("Any user is not found. userDN = {0}", settings.UserDN);
                return(USERS_NOT_FOUND);
            }
            foreach (var user in importer.AllDomainUsers)
            {
                if (!CheckLoginAttribute(user, settings.LoginAttribute))
                {
                    return(WRONG_LOGIN_ATTRIBUTE);
                }
                if (user.Sid == null)
                {
                    return(WRONG_SID_ATTRIBUTE);
                }
            }
            if (settings.GroupMembership)
            {
                if (!CheckGroupDN(settings.GroupDN, settings.Server, settings.PortNumber,
                                  settings.Authentication, settings.Login, password, settings.StartTls))
                {
                    return(WRONG_GROUP_DN);
                }
                try
                {
                    importer.DomainGroups = novellLdapHelper.GetGroupsByAttributes(settings);
                }
                catch (Exception ex)
                {
                    log.ErrorFormat("Incorrect filter. groupFilter = {0}, {1}", settings.GroupFilter, ex);
                    return(INCORRECT_GROUP_LDAP_FILTER);
                }
                if (importer.DomainGroups == null || importer.DomainGroups.Count == 0)
                {
                    log.ErrorFormat("Any group is not found. groupDN = {0}", settings.GroupDN);
                    return(GROUPS_NOT_FOUND);
                }
                foreach (var group in importer.DomainGroups)
                {
                    if (!CheckGroupAttribute(group, settings.GroupAttribute))
                    {
                        return(WRONG_GROUP_ATTRIBUTE);
                    }
                    if (!CheckGroupNameAttribute(group, settings.GroupNameAttribute))
                    {
                        return(WRONG_GROUP_NAME_ATTRIBUTE);
                    }
                    if (group.Sid == null)
                    {
                        return(WRONG_SID_ATTRIBUTE);
                    }
                }
                foreach (var user in importer.AllDomainUsers)
                {
                    if (!CheckUserAttribute(user, settings.UserAttribute))
                    {
                        return(WRONG_USER_ATTRIBUTE);
                    }
                }
            }
            return(OPERATION_OK);
        }
예제 #3
0
        public void RunJob(DistributedTask _, CancellationToken cancellationToken)
        {
            try
            {
                CancellationToken = cancellationToken;

                CoreContext.TenantManager.SetCurrentTenant(CurrentTenant);

                SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem);

                Thread.CurrentThread.CurrentCulture   = CultureInfo.GetCultureInfo(_culture);
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(_culture);

                Logger = LogManager.GetLogger(typeof(LDAPOperation));

                if (LDAPSettings == null)
                {
                    Error = Resource.LdapSettingsErrorCantGetLdapSettings;
                    Logger.Error("Can't save default LDAP settings.");
                    return;
                }

                if (OperationType == LDAPOperationType.Save)
                {
                    SetProgress(1, Resource.LdapSettingsStatusCheckingLdapSettings);

                    PrepareSettings(LDAPSettings);

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

                    Importer = new LDAPUserImporter(LDAPSettings);

                    SetProgress(5, Resource.LdapSettingsStatusLoadingBaseInfo);

                    var acceptCertificate = TaskInfo.GetProperty <bool>(CERT_ALLOW);

                    var result = _ldapSettingsChecker.CheckSettings(Importer, acceptCertificate);

                    if (result == LdapSettingsChecker.CERTIFICATE_REQUEST)
                    {
                        TaskInfo.SetProperty(FINISHED, true);

                        TaskInfo.SetProperty(CERT_REQUEST, ((NovellLdapSettingsChecker)_ldapSettingsChecker).CertificateConfirmRequest);

                        SetProgress(0, Resource.LdapSettingsStatusCertificateVerification);

                        return;
                    }

                    var error = GetError(result);
                    if (!string.IsNullOrEmpty(error))
                    {
                        Error = error;
                        return;
                    }

                    SetProgress(10, Resource.LdapSettingsStatusSavingSettings);

                    LDAPSettings.IsDefault = LDAPSettings.Equals(LDAPSettings.GetDefault());

                    if (!SettingsManager.Instance.SaveSettings(LDAPSettings, CurrentTenant.TenantId))
                    {
                        Logger.Error("Can't save LDAP settings.");
                        Error = Resource.LdapSettingsErrorCantSaveLdapSettings;
                        return;
                    }
                }
                else if (OperationType == LDAPOperationType.Sync)
                {
                    Importer = new LDAPUserImporter(LDAPSettings);
                }

                Do();
            }
            catch (AuthorizingException authError)
            {
                Error = Resource.ErrorAccessDenied;
                Logger.Error(Error, new SecurityException(Error, authError));
            }
            catch (AggregateException ae)
            {
                ae.Flatten().Handle(e => e is TaskCanceledException || e is OperationCanceledException);
            }
            catch (TenantQuotaException e)
            {
                Error = Resource.LdapSettingsTenantQuotaSettled;
                Logger.ErrorFormat("TenantQuotaException. {0}", e);
            }
            catch (FormatException e)
            {
                Error = Resource.LdapSettingsErrorCantCreateUsers;
                Logger.ErrorFormat("FormatException error. {0}", e);
            }
            catch (Exception e)
            {
                Error = Resource.LdapSettingsInternalServerError;
                Logger.ErrorFormat("Internal server error. {0}", e);
            }
            finally
            {
                try
                {
                    TaskInfo.SetProperty(FINISHED, true);
                    PublishTaskInfo();
                }
                catch { /* ignore */ }
            }
        }
예제 #4
0
        public static bool TryGetLdapUserInfo(string login, string password, out UserInfo userInfo)
        {
            userInfo = ASC.Core.Users.Constants.LostUser;

            if (!SetupInfo.IsVisibleSettings(ManagementType.LdapSettings.ToString()) ||
                CoreContext.Configuration.Standalone && !CoreContext.TenantManager.GetTenantQuota(TenantProvider.CurrentTenantID).Ldap)
            {
                return(false);
            }

            var settings = SettingsManager.Instance.LoadSettings <LDAPSupportSettings>(TenantProvider.CurrentTenantID);

            if (!settings.EnableLdapAuthentication)
            {
                return(false);
            }
            try
            {
                var importer = new LDAPUserImporter();
                try
                {
                    LdapSettingsChecker ldapSettingsChecker;
                    string currentLogin;
                    if (!WorkContext.IsMono)
                    {
                        ldapSettingsChecker = new SystemLdapSettingsChecker();
                        currentLogin        = login;
                    }
                    else
                    {
                        currentLogin = GetEntryDN(settings, login);
                        if (currentLogin == null)
                        {
                            return(false);
                        }
                        ldapSettingsChecker = new NovellLdapSettingsChecker();
                    }
                    ldapSettingsChecker.CheckCredentials(currentLogin, password, settings.Server, settings.PortNumber, settings.StartTls);
                }
                catch (Exception)
                {
                    return(false);
                }

                if (login.Contains("\\"))
                {
                    login = login.Split('\\')[1];
                }
                var sid = importer.GetSidOfCurrentUser(login, settings);
                if (sid == null)
                {
                    return(false);
                }
                List <GroupInfo> existingGroups;
                importer.GetDiscoveredGroupsByAttributes(settings, out existingGroups);
                if (importer.GetDiscoveredUser(settings, sid).Equals(ASC.Core.Users.Constants.LostUser))
                {
                    return(false);
                }

                userInfo = CoreContext.UserManager.GetUserBySid("l" + sid);
                if (userInfo.Equals(ASC.Core.Users.Constants.LostUser))
                {
                    userInfo = CoreContext.UserManager.GetUserBySid(sid);
                    if (userInfo.Equals(ASC.Core.Users.Constants.LostUser))
                    {
                        userInfo = importer.GetDiscoveredUser(settings, sid);
                        if (userInfo.Equals(ASC.Core.Users.Constants.LostUser))
                        {
                            return(false);
                        }
                        if (userInfo.FirstName == string.Empty)
                        {
                            userInfo.FirstName = Resource.FirstName;
                        }
                        if (userInfo.LastName == string.Empty)
                        {
                            userInfo.LastName = Resource.LastName;
                        }
                        try
                        {
                            SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem);

                            var asVisitor = TenantStatisticsProvider.GetUsersCount() >= TenantExtra.GetTenantQuota().ActiveUsers;
                            userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false, asVisitor);

                            importer.AddUserIntoGroups(userInfo, settings);
                            importer.AddUserInCacheGroups(userInfo);
                        }
                        finally
                        {
                            SecurityContext.Logout();
                        }
                    }
                }
                else
                {
                    userInfo.Sid = sid;
                    try
                    {
                        SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem);

                        var asVisitor = TenantStatisticsProvider.GetUsersCount() >= TenantExtra.GetTenantQuota().ActiveUsers;

                        userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false, asVisitor, false, false);
                    }
                    finally
                    {
                        SecurityContext.Logout();
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                log.ErrorFormat("Unexpected error: {0}", e);
                userInfo = ASC.Core.Users.Constants.LostUser;
                return(false);
            }
        }
예제 #5
0
        public override byte CheckSettings(LDAPUserImporter importer,
                                           bool acceptCertificate = false)
        {
            var settings = importer.Settings;

            if (!settings.EnableLdapAuthentication)
            {
                return(OPERATION_OK);
            }

            var password = GetPassword(settings.PasswordBytes);

            try
            {
                if (settings.Authentication)
                {
                    CheckCredentials(settings.Login, password, settings.Server, settings.PortNumber, settings.StartTls);
                }
                if (!CheckServerAndPort(settings.Server,
                                        settings.PortNumber, settings.Authentication, settings.Login, password))
                {
                    return(WRONG_SERVER_OR_PORT);
                }
            }
            catch (DirectoryServicesCOMException)
            {
                return(CREDENTIALS_NOT_VALID);
            }
            catch (COMException)
            {
                return(WRONG_SERVER_OR_PORT);
            }

            if (!CheckUserDN(settings.UserDN, settings.Server, settings.PortNumber,
                             settings.Authentication, settings.Login, password, settings.StartTls))
            {
                return(WRONG_USER_DN);
            }

            if (settings.GroupMembership)
            {
                if (!CheckGroupDN(settings.UserDN, settings.Server, settings.PortNumber,
                                  settings.Authentication, settings.Login, password, settings.StartTls))
                {
                    return(WRONG_USER_DN);
                }

                if (!importer.TryLoadLDAPGroups())
                {
                    return(INCORRECT_GROUP_LDAP_FILTER);
                }

                if (!importer.AllDomainGroups.Any())
                {
                    return(GROUPS_NOT_FOUND);
                }

                foreach (var group in importer.AllDomainGroups)
                {
                    if (!CheckGroupAttribute(group, settings.GroupAttribute))
                    {
                        return(WRONG_GROUP_ATTRIBUTE);
                    }

                    if (!CheckGroupNameAttribute(group, settings.GroupNameAttribute))
                    {
                        return(WRONG_GROUP_NAME_ATTRIBUTE);
                    }

                    if (group.Sid == null)
                    {
                        return(WRONG_SID_ATTRIBUTE);
                    }
                }
            }

            if (!importer.TryLoadLDAPDomain())
            {
                return(DOMAIN_NOT_FOUND);
            }

            if (!importer.TryLoadLDAPUsers())
            {
                return(INCORRECT_LDAP_FILTER);
            }

            if (!importer.AllDomainUsers.Any())
            {
                return(USERS_NOT_FOUND);
            }

            foreach (var user in importer.AllDomainUsers)
            {
                if (!CheckLoginAttribute(user, settings.LoginAttribute))
                {
                    return(WRONG_LOGIN_ATTRIBUTE);
                }

                if (user.Sid == null)
                {
                    return(WRONG_SID_ATTRIBUTE);
                }

                if (settings.GroupMembership && !CheckUserAttribute(user, settings.UserAttribute))
                {
                    return(WRONG_USER_ATTRIBUTE);
                }
            }

            return(OPERATION_OK);
        }
        public static bool TryGetLdapUserInfo(string login, string password, out UserInfo userInfo)
        {
            userInfo = ASC.Core.Users.Constants.LostUser;

            try
            {
                if (!LdapIsEnable)
                {
                    return(false);
                }

                var settings = SettingsManager.Instance.LoadSettings <LDAPSupportSettings>(TenantProvider.CurrentTenantID);
                if (!settings.EnableLdapAuthentication)
                {
                    return(false);
                }

                var importer = new LDAPUserImporter(settings);

                var ldapUserInfo = ASC.Core.Users.Constants.LostUser;
                try
                {
                    var ldapSettingsChecker = WorkContext.IsMono
                        ? new NovellLdapSettingsChecker()
                        : new SystemLdapSettingsChecker() as LdapSettingsChecker;

                    var parsedLogin = ldapSettingsChecker.ParseLogin(login);

                    var ldapUsers = importer.FindLdapUsers(parsedLogin);

                    foreach (var ldapUser in ldapUsers)
                    {
                        try
                        {
                            ldapUserInfo = ldapUser.Key;
                            var ldapUserObject = ldapUser.Value;

                            if (ldapUserInfo.Equals(ASC.Core.Users.Constants.LostUser) ||
                                ldapUserObject == null ||
                                string.IsNullOrEmpty(ldapUserObject.DistinguishedName))
                            {
                                continue;
                            }

                            string currentLogin;

                            if (!WorkContext.IsMono)
                            {
                                currentLogin =
                                    ldapUserObject.InvokeGet(Constants.ADSchemaAttributes.ACCOUNT_NAME) as string;
                            }
                            else
                            {
                                currentLogin = ldapUserObject.DistinguishedName;
                            }

                            ldapSettingsChecker.CheckCredentials(currentLogin, password, settings.Server,
                                                                 settings.PortNumber,
                                                                 settings.StartTls);

                            break;
                        }
                        catch (Exception)
                        {
                            ldapUserInfo = ASC.Core.Users.Constants.LostUser;
                        }
                    }

                    if (ldapUserInfo.Equals(ASC.Core.Users.Constants.LostUser))
                    {
                        return(false);
                    }
                }
                catch (Exception)
                {
                    return(false);
                }

                if (settings.GroupMembership && !importer.IsUserExistsInGroups(ldapUserInfo))
                {
                    return(false);
                }

                try
                {
                    SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem);

                    userInfo = UserManagerWrapper.SyncUserLDAP(ldapUserInfo);

                    if (userInfo == null || userInfo.Equals(ASC.Core.Users.Constants.LostUser))
                    {
                        return(false);
                    }

                    userInfo.Sid = ldapUserInfo.Sid;

                    importer.SyncUserGroupMembership(userInfo);
                }
                finally
                {
                    SecurityContext.Logout();
                }

                return(true);
            }
            catch (Exception e)
            {
                Log.ErrorFormat("TryGetLdapUserInfo(login: '******') Unexpected error: {1}", login, e);
                userInfo = ASC.Core.Users.Constants.LostUser;
                return(false);
            }
        }
예제 #7
0
        public override byte CheckSettings(LDAPSupportSettings settings, LDAPUserImporter importer, bool acceptCertificate = false)
        {
            if (!settings.EnableLdapAuthentication)
            {
                return(OPERATION_OK);
            }

            string password = GetPassword(settings.PasswordBytes);

            try
            {
                if (settings.Authentication)
                {
                    CheckCredentials(settings.Login, password, settings.Server, settings.PortNumber, settings.StartTls);
                }
                if (!CheckServerAndPort(settings.Server,
                                        settings.PortNumber, settings.Authentication, settings.Login, password))
                {
                    return(WRONG_SERVER_OR_PORT);
                }
            }
            catch (DirectoryServicesCOMException)
            {
                return(CREDENTIALS_NOT_VALID);
            }
            catch (COMException)
            {
                return(WRONG_SERVER_OR_PORT);
            }

            if (!CheckUserDN(settings.UserDN, settings.Server, settings.PortNumber,
                             settings.Authentication, settings.Login, password, settings.StartTls))
            {
                return(WRONG_USER_DN);
            }
            try
            {
                importer.AllDomainUsers = ldapHelper.GetUsersByAttributes(settings);
            }
            catch (ArgumentException)
            {
                log.ErrorFormat("Incorrect filter. userFilter = {0}", settings.UserFilter);
                return(INCORRECT_LDAP_FILTER);
            }
            if (importer.AllDomainUsers == null || importer.AllDomainUsers.Count == 0)
            {
                log.ErrorFormat("Any user is not found. userDN = {0}", settings.UserDN);
                return(USERS_NOT_FOUND);
            }
            foreach (var user in importer.AllDomainUsers)
            {
                if (!CheckLoginAttribute(user, settings.LoginAttribute))
                {
                    return(WRONG_LOGIN_ATTRIBUTE);
                }
            }
            if (settings.GroupMembership)
            {
                if (!CheckGroupDN(settings.UserDN, settings.Server, settings.PortNumber,
                                  settings.Authentication, settings.Login, password, settings.StartTls))
                {
                    return(WRONG_USER_DN);
                }
                try
                {
                    importer.DomainGroups = ldapHelper.GetGroupsByAttributes(settings);
                }
                catch (ArgumentException)
                {
                    log.ErrorFormat("Incorrect group filter. groupFilter = {0}", settings.GroupFilter);
                    return(INCORRECT_GROUP_LDAP_FILTER);
                }
                if (importer.DomainGroups == null || importer.DomainGroups.Count == 0)
                {
                    return(GROUPS_NOT_FOUND);
                }
                foreach (var group in importer.DomainGroups)
                {
                    if (!CheckGroupAttribute(group, settings.GroupAttribute))
                    {
                        return(WRONG_GROUP_ATTRIBUTE);
                    }
                    if (!CheckGroupNameAttribute(group, settings.GroupNameAttribute))
                    {
                        return(WRONG_GROUP_NAME_ATTRIBUTE);
                    }
                }
                foreach (var user in importer.AllDomainUsers)
                {
                    if (!CheckUserAttribute(user, settings.UserAttribute))
                    {
                        return(WRONG_USER_ATTRIBUTE);
                    }
                }
            }
            return(OPERATION_OK);
        }
예제 #8
0
 public abstract byte CheckSettings(LDAPUserImporter importer, bool acceptCertificate = false);
예제 #9
0
        public void RunJob()
        {
            try
            {
                SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem);

                var tenantId = (int)Id;

                CoreContext.TenantManager.SetCurrentTenant(tenantId);

                ResolveCulture();

                SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 1);
                SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsStatusCheckingLdapSettings);
                SetProggressInfo(Constants.LDAP_SETTING_TASK_IS_COMPLETED, false);

                if (_settings == null)
                {
                    _log.Error("Can't save default LDAP settings.");
                    SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR, Resource.LdapSettingsErrorCantGetLdapSettings);
                    return;
                }
                try
                {
                    PrepareSettings(_settings);

                    var acceptCertificate =
                        (bool?)AscCache.Default.Get <object>(Constants.LDAP_SETTING_TASK_ACCEPT_CERTIFICATE) ?? false;

                    if (_importer == null)
                    {
                        _importer = new LDAPUserImporter(_settings);
                    }

                    SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 5);
                    SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsStatusLoadingBaseInfo);

                    var result = _ldapSettingsChecker.CheckSettings(_importer, acceptCertificate);

                    if (result == LdapSettingsChecker.CERTIFICATE_REQUEST)
                    {
                        SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 0);
                        SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS,
                                         Resource.LdapSettingsStatusCertificateVerification);
                        SetProggressInfo(Constants.NOVELL_LDAP_CERTIFICATE_CONFIRM_REQUEST,
                                         ((NovellLdapSettingsChecker)_ldapSettingsChecker).CertificateConfirmRequest);
                        return;
                    }

                    var error = GetError(result);
                    if (error != string.Empty)
                    {
                        SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR, error);
                        return;
                    }

                    if (!_skipSaveSettings)
                    {
                        SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsStatusSavingSettings);
                        SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 10);

                        _settings.IsDefault = _settings.Equals(_settings.GetDefault());

                        if (!SettingsManager.Instance.SaveSettings(_settings, tenantId))
                        {
                            _log.Error("Can't save LDAP settings.");
                            SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR,
                                             Resource.LdapSettingsErrorCantSaveLdapSettings);
                            return;
                        }
                    }

                    RemoveOldWorkaroundOnLogoutLDAPUsers();

                    if (_settings.EnableLdapAuthentication)
                    {
                        SyncLDAP();

                        var taskError = AscCache.Default.Get <string>(Constants.LDAP_SETTING_TASK_ERROR);
                        if (!string.IsNullOrEmpty(taskError))
                        {
                            return;
                        }
                    }
                    else
                    {
                        SetProggressInfo(Constants.LDAP_SETTING_TASK_STATUS, Resource.LdapSettingsModifyLdapUsers);
                        SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 50);

                        var existingLDAPUsers = CoreContext.UserManager.GetUsers().Where(u => u.Sid != null).ToList();
                        foreach (var existingLDAPUser in existingLDAPUsers)
                        {
                            existingLDAPUser.Sid = null;
                            CoreContext.UserManager.SaveUserInfo(existingLDAPUser);
                        }
                    }

                    SetProggressInfo(Constants.LDAP_SETTING_TASK_PERCENTAGE, 100);
                }
                catch (TenantQuotaException e)
                {
                    _log.ErrorFormat("TenantQuotaException. {0}", e);
                    SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR, Resource.LdapSettingsTenantQuotaSettled);
                }
                catch (FormatException e)
                {
                    _log.ErrorFormat("FormatException error. {0}", e);
                    SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR, Resource.LdapSettingsErrorCantCreateUsers);
                }
                catch (Exception e)
                {
                    _log.ErrorFormat("Internal server error. {0}", e);
                    SetProggressInfo(Constants.LDAP_SETTING_TASK_ERROR, Resource.LdapSettingsInternalServerError);
                }
            }
            finally
            {
                SetProggressInfo(Constants.LDAP_SETTING_TASK_IS_COMPLETED, true);
                AscCache.Default.Remove(Constants.LDAP_SETTING_TASK_STARTED);
                SecurityContext.Logout();
            }
        }
        public static bool TryLdapAuth(string login, string password)
        {
            if (!SetupInfo.IsVisibleSettings(ManagementType.LdapSettings.ToString()))
            {
                return(false);
            }

            var settings = SettingsManager.Instance.LoadSettings <LDAPSupportSettings>(TenantProvider.CurrentTenantID);

            if (!settings.EnableLdapAuthentication)
            {
                return(false);
            }
            try
            {
                var importer = new LDAPUserImporter();
                try
                {
                    ADDomain.CheckCredentials(login, password, settings.Server, settings.PortNumber);
                }
                catch (Exception)
                {
                    return(false);
                }

                var sid = importer.GetSidOfCurrentUser(login, settings);
                if (sid == null)
                {
                    return(false);
                }
                importer.GetDiscoveredGroupsByAttributes(settings);
                var userInfo = CoreContext.UserManager.GetUserBySid("l" + sid);
                if (userInfo == ASC.Core.Users.Constants.LostUser)
                {
                    userInfo = CoreContext.UserManager.GetUserBySid(sid);
                    if (userInfo == ASC.Core.Users.Constants.LostUser)
                    {
                        userInfo = importer.GetDiscoveredUser(settings, sid);
                        if (userInfo == ASC.Core.Users.Constants.LostUser)
                        {
                            return(false);
                        }
                        try
                        {
                            SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem);
                            if (TenantStatisticsProvider.GetUsersCount() < TenantExtra.GetTenantQuota().ActiveUsers)
                            {
                                userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false);
                            }
                            else
                            {
                                userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false, true);
                            }
                            importer.AddUserIntoGroups(userInfo, settings);
                            importer.AddUserInCacheGroups(userInfo);
                        }
                        finally
                        {
                            SecurityContext.Logout();
                        }
                    }
                }
                else
                {
                    if (importer.GetDiscoveredUser(settings, sid) == ASC.Core.Users.Constants.LostUser)
                    {
                        return(false);
                    }
                    userInfo.Sid = sid;
                    try
                    {
                        SecurityContext.AuthenticateMe(ASC.Core.Configuration.Constants.CoreSystem);
                        if (TenantStatisticsProvider.GetUsersCount() < TenantExtra.GetTenantQuota().ActiveUsers)
                        {
                            userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false);
                        }
                        else
                        {
                            userInfo = UserManagerWrapper.AddUser(userInfo, UserManagerWrapper.GeneratePassword(), true, false, true);
                        }
                    }
                    finally
                    {
                        SecurityContext.Logout();
                    }
                }
                var cookiesKey = SecurityContext.AuthenticateMe(userInfo.ID);
                CookiesManager.SetCookies(CookiesType.AuthKey, cookiesKey);
                return(true);
            }
            catch (Exception e)
            {
                ADDomain.LogError(e.Message);
                return(false);
            }
        }