Esempio n. 1
0
        public ActionResult Index(LoginViewModel model)
        {
            if (!ModelState.IsValid)
                return View(model);

            if (_userStore.Authenticate(model.Username, model.Password))
            {
                _authenticationService.SetAuthCookie(model.Username, model.RememberMe);
                return RedirectToAction("Index", "Home");
            }

            ModelState.AddModelError("WrongUsernamePassword", "Invalid Username and Password");

            return View(model);
        }
        public void Login_With_Valid_User()
        {
            var userStore = new Mock<IUserStore>();
            var authService = new Mock<IFormsAuthenticationService>();
            userStore.Setup(u => u.Authenticate("abc", "def")).Returns(true);
            authService.Setup(a => a.SetAuthCookie("abc", false));
            var controller = new UserController(userStore.Object, authService.Object);
            var model = new LoginViewModel
            {
                Password = "******",
                Username = "******"
            };

            var result = controller.Index(model);

            userStore.Verify(u => u.Authenticate("abc", "def"), Times.Once);
            authService.Verify(a => a.SetAuthCookie("abc", false), Times.Once);
            var redirect = (RedirectToRouteResult)result;
            Assert.AreEqual("Index", redirect.RouteValues["action"]);
            Assert.AreEqual("Home", redirect.RouteValues["controller"]);
        }
        public void Login_With_Invalid_User()
        {
            var userStore = new Mock<IUserStore>();
            var authService = new Mock<IFormsAuthenticationService>();
            userStore.Setup(u => u.Authenticate("abc", "def")).Returns(false);
            var controller = new UserController(userStore.Object, authService.Object);
            var model = new LoginViewModel
            {
                Password = "******",
                Username = "******"
            };

            var result = controller.Index(model);

            userStore.Verify(u => u.Authenticate("abc", "def"), Times.Once);
            Assert.IsInstanceOf(typeof(ViewResult), result);
            var view = (ViewResult)result;
            Assert.IsInstanceOf(typeof(LoginViewModel), view.Model);
            model = (LoginViewModel)view.Model;
            Assert.AreEqual("abc", model.Username);
            Assert.AreEqual("def", model.Password);
        }
Esempio n. 4
0
 public ActionResult Index()
 {
     var model = new LoginViewModel();
     return View(model);
 }