Exemplo n.º 1
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            userManager = new UserManager(new UserStore(new LogistoDb()), Startup.IdentityFactoryOptions, new EmailService());

            // Валидация входных данных: проверка на корректность
            var login            = model.Login;
            var loginValidity    = SignInHelper.ValidateLogin(ref login);
            var passwordValidity = userManager.PasswordValidator.ValidateAsync(model.Password).Result;

            if (!loginValidity.Succeeded || !passwordValidity.Succeeded)
            {
                ModelState.AddModelError("", "LoginFormWrongInputMessage");
                return(View(model));
            }

            // This doen't count login failures towards lockout only two factor authentication
            // To enable password failures to trigger lockout, change to shouldLockout: true

            var result = await new SignInHelper(userManager, AuthenticationManager).PasswordSignIn(login, model.Password, /*model.RememberMe*/ true, shouldLockout: false);

            switch (result)
            {
            case Logisto.Identity.SignInStatus.Success:
                if (model.Password == "123456789")
                {
                    return(RedirectToAction("ChangePassword"));
                }

                if (!string.IsNullOrWhiteSpace(returnUrl))
                {
                    return(Redirect(returnUrl));
                }
                else
                {
                    return(RedirectToAction("Index"));
                }

            case Logisto.Identity.SignInStatus.LockedOut:
                return(View("Lockout"));

            case Logisto.Identity.SignInStatus.RequiresTwoFactorAuthentication:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl }));

            case Logisto.Identity.SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Неверный логин или пароль");
                return(View(model));
            }
        }