Example #1
0
 /// <summary>
 /// Конструктор
 /// </summary>
 /// <param name="ambientContext"></param>
 /// <param name="application"></param>
 /// <param name="signInManager"></param>
 /// <param name="userSearcher"></param>
 /// <param name="passwordHashValidator"></param>
 /// <param name="authenticationManager"></param>
 /// <param name="logger"></param>
 public AccountLoginService(ICrocoAmbientContextAccessor ambientContext,
                            ICrocoApplication application,
                            SignInManager <ApplicationUser> signInManager,
                            UserSearcher userSearcher,
                            PasswordHashValidator passwordHashValidator,
                            IApplicationAuthenticationManager authenticationManager,
                            ILogger <AccountLoginService> logger) : base(ambientContext, application, logger)
 {
     SignInManager         = signInManager;
     UserSearcher          = userSearcher;
     PasswordHashValidator = passwordHashValidator;
     AuthenticationManager = authenticationManager;
 }
Example #2
0
        /// <summary>
        /// Авторизоваться по Email
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <LoginResultModel> LoginAsync(LoginModel model)
        {
            var validation = ValidateModel(model);

            if (!validation.IsSucceeded)
            {
                return(new LoginResultModel(LoginErrorType.ModelNotValid, validation.Message));
            }

            if (IsAuthenticated)
            {
                return(new LoginResultModel(LoginErrorType.AlreadyAuthenticated, "Вы уже авторизованы в системе"));
            }

            model.RememberMe = true;

            var user = await SignInManager.UserManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                Logger.LogTrace("user is null");
                return(UnSuccessfulLoginAttempt());
            }

            if (user.Email == RootSettings.RootEmail)
            {
                Logger.LogTrace("AccountLoginWorker.LoginAsync.RootCase");

                if (model.Password != RootSettings.RootPassword)
                {
                    return(UnSuccessfulLoginAttempt());
                }

                await SignInManager.SignInAsync(user, model.RememberMe);

                return(SuccessfulLoginResult());
            }

            var checkClientResult = await CheckClient(model.Email);

            if (!checkClientResult.Succeeded)
            {
                return(checkClientResult);
            }

            var accountSettings = GetSetting <AccountSettingsModel>();

            //если логинирование не разрешено для пользователей которые не подтвердили Email и у пользователя Email не потверждён
            if (!user.EmailConfirmed && !accountSettings.IsLoginEnabledForUsersWhoDidNotConfirmEmail)
            {
                return(new LoginResultModel(LoginErrorType.EmailNotConfirmed, "Ваш Email не подтверждён"));
            }

            try
            {
                //проверяю пароль
                var passCheckResult = await PasswordHashValidator
                                      .CheckUserNameAndPasswordAsync(user.Id, user.UserName, model.Password);

                Logger.LogTrace("AccountLoginService.LoginAsync.PassCheckResult", passCheckResult);

                //если пароль не подходит выдаю ответ
                if (!passCheckResult.IsSucceeded)
                {
                    return(UnSuccessfulLoginAttempt());
                }

                await SignInManager.SignInAsync(user, model.RememberMe);

                return(SuccessfulLoginResult());
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "AccountLoginWorker.LoginAsync.OnException");

                return(new LoginResultModel(LoginErrorType.Error, ex.Message));
            }
        }