コード例 #1
0
        public ActionResult Login(AccountModelView model, string returnUrl)
        {
            if(ModelState.IsValid)
            {
                if(_authProvider.Authenticate(model.UserName, model.Password))
                    return Redirect(returnUrl ?? Url.Action("Index", "Admin"));

                ModelState.AddModelError("", "Неправильный логин или пароль");

                return View();
            }

            return View();
        }
コード例 #2
0
        public void Can_Login_With_Valid_Credentials()
        {
            var mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate("admin", "12345")).Returns(true);

            var model = new AccountModelView
            {
                UserName = "******",
                Password = "******"
            };

            var target = new AccountController(mock.Object);

            var result = target.Login(model, "/MyURL");

            Assert.IsInstanceOfType(result, typeof(RedirectResult));
            Assert.AreEqual("/MyURL", ((RedirectResult)result).Url);
        }
コード例 #3
0
        public void Cannot_Login_With_Invalid_Credentials()
        {
            var mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate("badUser", "badPass")).Returns(false);

            var model = new AccountModelView
            {
                UserName = "******",
                Password = "******"
            };

            var target = new AccountController(mock.Object);

            var result = target.Login(model, "/MyURL");

            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.IsFalse(((ViewResult)result).ViewData.ModelState.IsValid);
        }