public ActionResult LogOn(AccountLogOnModel model)
        {
            if (ModelState.IsValid)
            {
                model.Email = model.Email.ToLower().Trim();

                if (_membershipService.ValidateUser(model.Email, model.Password))
                {
                    _formsService.SignIn(model.Email, model.RememberMe);
                    if (!string.IsNullOrEmpty(model.ReturnUrl))
                        return Redirect(model.ReturnUrl);

                    return RedirectTo<HomeController>(c => c.Index());
                }

                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public void LogOn_Post_ReturnsViewIfModelStateIsInvalid()
        {
            // Arrange
            var model = new AccountLogOnModel
            {
                Email = "*****@*****.**",
                Password = "******",
                RememberMe = false
            };
            _accountController.ModelState.AddModelError("", "Dummy error message.");

            // Act
            ActionResult result = _accountController.LogOn(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            var viewResult = (ViewResult)result;
            Assert.AreEqual(model, viewResult.ViewData.Model);
        }
        public void LogOn_Post_ReturnsViewIfValidateUserFails()
        {
            // Arrange
            var model = new AccountLogOnModel
            {
                Email = "*****@*****.**",
                Password = "******",
                RememberMe = false
            };

            // Act
            ActionResult result = _accountController.LogOn(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            var viewResult = (ViewResult)result;
            Assert.AreEqual(model, viewResult.ViewData.Model);
            Assert.AreEqual("The user name or password provided is incorrect.", _accountController.ModelState[""].Errors[0].ErrorMessage);
        }
        public void LogOn_Post_ReturnsRedirectOnSuccess_WithReturnUrl()
        {
            // Arrange
            var model = new AccountLogOnModel
            {
                Email = "*****@*****.**",
                Password = "******",
                RememberMe = false,
                ReturnUrl = "/someUrl"
            };

            // Act
            ActionResult result = _accountController.LogOn(model);

            // Assert
            Assert.IsInstanceOfType(result, typeof(RedirectResult));
            var redirectResult = (RedirectResult)result;
            Assert.AreEqual("/someUrl", redirectResult.Url);
            Assert.IsTrue(_authenticationService.SignIn_WasCalled);
        }