public void LogOn()
        {
            // Arrange - database
            /*Mock<AccountRepository> mock = new Mock<AccountRepository>();
            mock.Setup(u => u.Save(new Account {
                ID = 1,
                Email = "*****@*****.**",
                Password = "******",
                FirstName = "Mr.",
                LastName = "Whatever",
                Role = 2,
                CreatedDate = DateTime.Now
            }));*/

            IAccountRepository accountRepository = new AccountRepository();
            accountRepository.Save(new Account { ID = 1, Email = "*****@*****.**", FirstName = "Admin", LastName = "von Världsklass", Password = "******", Salt = "oh yeah", Administrator = true, CreatedDate = DateTime.Now });
            accountRepository.Save(new Account { ID = 2, Email = "*****@*****.**", FirstName = "Bokare", LastName = "von Världsklass", Password = "******", Salt = "good salt", Administrator = false, CreatedDate = DateTime.Now });

            // Arrange - viewmodel
            LogOnViewModel model = new LogOnViewModel
            {
                Email = "*****@*****.**",
                Password = "******"
            };

            // Arrange - controller
            AccountController controller = new AccountController(accountRepository);

            // Act
            ActionResult result = controller.LogOn(model, "/") as ActionResult;

            // Assert
            Assert.IsInstanceOfType(result, typeof(RedirectResult));
            Assert.AreEqual("/", ((RedirectResult)result).Url);
        }
Пример #2
0
        public ActionResult LogOn(LogOnViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (membership.ValidateUser(model.Email, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        if(IsAdmin(model.Email))
                            return RedirectToAction("Index", "Admin");
                        else
                            return RedirectToAction("Index", "Home");

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

            // If we got this far, something failed, redisplay form
            return View(model);
        }