Exemplo n.º 1
0
		public void LoginAction_invalid_login_or_password_should_return_view_with_error()
		{
			UsersManagement.CheckUserPassword(Arg.Is<string>("test1"), Arg.Is<string>("testpass1")).Returns(false);
			UserAccessHlp.IsUserInactive().Returns<bool>(true);
			Target.CommitProviderInstance = Substitute.For<ICommitProvider>();

			var model = new SignInViewModel() { Email = "test1", Password = "******", RememberUser = false };

			var actionResult = Target.Login(model, null);
			Target.ModelState.Keys.Should().Contain("InvalidUserDataError");
			actionResult.Should().NotBeNull().And.BeOfType<ViewResult>();
		}
Exemplo n.º 2
0
		public ActionResult Login(SignInViewModel model, string returnUrl)
		{
			if (!ModelState.IsValid)
			{
				return View(NameHelper.Home.Index);
			}
			var email = model.Email.Trim();

			var checkFailedAttempt = _userManagement.CheckFailedAttempts(email);
			if (checkFailedAttempt == FailedAttemptResult.Unlock)
			{
				_userManagement.ClearFailedAttempt(email);
				CommitProviderInstance.Commit();
			}
			if (checkFailedAttempt == FailedAttemptResult.Locked)
			{
				ModelState.AddModelError("InvalidUserDataError", string.Format("Your account has been locked for {0} minutes for security reasons. Please, wait and try again, or contact site administrator.", _userManagement.GetUserLockOutTime()));
				return View(NameHelper.Home.Index);
			}

			var result = _userManagement.CheckUserPassword(email, model.Password);
			if (!result)
			{
				_userManagement.AddFailedAttempt(email, DateTime.Now);
				CommitProviderInstance.Commit();
				ModelState.AddModelError("InvalidUserDataError", "E-mail and/or password is incorrect. Please, try again.");
				return View(NameHelper.Home.Index);
			}

			if (checkFailedAttempt != FailedAttemptResult.Unlock)
			{
				_userManagement.ClearFailedAttempt(email);
				CommitProviderInstance.Commit();
			}

			SecurityContext.SetCurrentUser(email);
			if (_userAccessHelper.IsUserInactive())
			{
				ModelState.AddModelError("InvalidUserDataError", "Sorry, your account is inactive.");
				return View(NameHelper.Home.Index);
			}

			_authenticationProvider.SetAuthCookie(email, model.RememberUser, Response);

			if (!String.IsNullOrWhiteSpace(returnUrl))
			{
				return Redirect(returnUrl);
			}

			return RedirectToAction(NameHelper.Home.Index, NameHelper.Home.Controller);
		}
Exemplo n.º 3
0
		public void LoginAction_with_not_empty_returnUrl_and_active_user_should_redirect()
		{
			UsersManagement.CheckUserPassword(Arg.Is<string>("test2"), Arg.Is<string>("testpass2")).Returns(true);
			UserAccessHlp.IsUserInactive().Returns<bool>(false);
			Target.CommitProviderInstance = Substitute.For<ICommitProvider>();
			var model = new SignInViewModel() { Email = "test2", Password = "******", RememberUser = false };

			var actionResult = Target.Login(model, "someurl");
			actionResult.Should().NotBeNull().And.BeOfType<RedirectResult>();
		}
Exemplo n.º 4
0
		public void LoginAction_inactive_user_shouldnot_login()
		{
			UsersManagement.CheckUserPassword(Arg.Is<string>("test2"), Arg.Is<string>("testpass2")).Returns(true);
			UserAccessHlp.IsUserInactive().Returns<bool>(true);
			Target.CommitProviderInstance = Substitute.For<ICommitProvider>();
			var model = new SignInViewModel() { Email = "test2", Password = "******", RememberUser = false };

			var actionResult = Target.Login(model, null);
			actionResult.Should().NotBeNull().And.BeOfType<ViewResult>();
			(actionResult as ViewResult).ViewData.ModelState.Should().NotBeEmpty();
			(actionResult as ViewResult).ViewData.ModelState["InvalidUserDataError"].Should().NotBeNull();
		}