public void Can_Login_With_Valid_Credentials()
        {
            // Arrange
            // - Create a mock authentication provider
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate("admin", "secret")).Returns(true);

            // Arrange
            // - Create the view model
            LoginViewModel model = new LoginViewModel
            {
                UserName = "******",
                Password = "******"
            };

            // Arrange
            // - Create the controller
            AccountController target = new AccountController(mock.Object);

            // Act
            // - Authenticate using valid credentials
            ActionResult result = target.Login(model, "/MyURL");

            // Assert
            Assert.IsInstanceOfType(result, typeof(RedirectResult));
            Assert.AreEqual("/MyURL", ((RedirectResult)result).Url);
        }
        public ActionResult Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (authProvider.Authenticate(model.UserName, model.Password) && model.UserName == "admin")
                {
                    return Redirect(Url.Action("Index", "Admin"));
                }

                using (EFDbContext dbc = new EFDbContext())
                {
                    var user = dbc.UserAccount.Where(u => u.Username == model.UserName && u.Password == model.Password).FirstOrDefault();
                    if (user != null)
                    {
                        FormsAuthentication.SetAuthCookie(user.Username, false);
                        return RedirectToAction("List","Product");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Incorrect username or password");
                        return View();
                    }
                }
            }
            else
            {
                return View();
            }
        }
        public void Cannot_Login_With_Invalid_Credentials()
        {
            // Arrange
            // - Create a mock authentication provier
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate("badUser", "badPass")).Returns(false);

            // Arrange
            // - Create the view model
            LoginViewModel model = new LoginViewModel
            {
                UserName = "******",
                Password = "******"
            };

            // Arrange
            // - Create the controller
            AccountController target = new AccountController(mock.Object);

            // Act
            // - Authenticate using valid credentials
            ActionResult result = target.Login(model, "/MyURL");

            // Assert
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.IsFalse(((ViewResult)result).ViewData.ModelState.IsValid);
        }
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
                return View();

            if (_auth.Authenticate(model.UserName, model.Password))
                return Redirect(returnUrl ?? Url.Action("Index", "Admin"));

            ModelState.AddModelError("", "Nie poprwany login lub hasło");
            return View();
        }
        public void Cannot_Login_With_Invalid_Credentials()
        {
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate("admin", "123456")).Returns(true);
            LoginViewModel model = new LoginViewModel { Password = "******", Username = "******" };
            AccountController target = new AccountController(mock.Object);

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

            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.IsFalse(((ViewResult)result).ViewData.ModelState.IsValid);
        }
        public void Cannot_Login_With_Invalid_Credentials()
        {
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate("badUser", "badPass")).Returns(false);
            LoginViewModel model = new LoginViewModel { UserName = "******", Password =  "******" };
            AccountController target = new AccountController(mock.Object);

            ActionResult result = target.Login(model, "/MyUrl");

            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.IsFalse(((ViewResult)result).ViewData.ModelState.IsValid);
        }
        public void Can_Login_With_Valid_Credentials()
        {
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate("admin", "secret")).Returns(true);
            LoginViewModel model = new LoginViewModel { UserName = "******", Password = "******" };
            AccountController target = new AccountController(mock.Object);

            ActionResult result = target.Login(model, "/MyUrl");

            Assert.IsInstanceOfType(result, typeof(RedirectResult));
            Assert.AreEqual("/MyUrl", ((RedirectResult)result).Url);
        }
 public ActionResult Login(LoginViewModel model, string returnUrl) {
     if (ModelState.IsValid) {
         if (authProvider.Authenticate(model.UserName, model.Password)) {
             return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
         } else {
             ModelState.AddModelError("", "Incorrect username or password");
             return View();
         }
     } else {
         return View();
     }
 }
        public void CannotLoginWithInvalidCredentials()
        {
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate("zlyAdmin", "zlySekret")).Returns(false);
            LoginViewModel model = new LoginViewModel() { UserName = "******", Password = "******" };
            AccountController controller = new AccountController(mock.Object);

            ActionResult result = controller.Login(model, "/MyUrl");

            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.IsFalse(((ViewResult)result).ViewData.ModelState.IsValid);
        }
 public ActionResult Login(LoginViewModel model, string returnUrl)
 {
     if (ModelState.IsValid)
     {
         if (authProvider.Authenticate(model.UserName, model.Password))
         {
             return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
         }
         ModelState.AddModelError("","Nieprawidłowa nazwa użytkownika lub niepoprawne hasło.");
         return View();
     }
     return View();
 }
        public void Cannot_Login_With_Invalid_Credentials()
        {
            var mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate("admin", "passowd")).Returns(true);
            var model = new LoginViewModel
            {
                UserName = "******",
                Password = "******"
            };

            var target = new AccountController(mock.Object);
            var result = target.Login(model, "myUrl");
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.AreEqual(false, ((ViewResult)result).ViewData.ModelState.IsValid);
        }
        public void Can_login_with_valid_credentials()
        {
            var mock = new Mock<IAuthProvider>();
            mock.Setup(p => p.Authenticate("adm", "sec")).Returns(true);

            var loginViewModel = new LoginViewModel
            {
                Username = "******",
                Password = "******"
            };

            var controller = new AccountController(mock.Object);
            var result = controller.Login(loginViewModel, "my-url");

            Assert.IsInstanceOfType(result, typeof(RedirectResult));
            Assert.AreEqual("my-url", ((RedirectResult)result).Url);
        }
        public void Cannot_login_with_bad_credentials()
        {
            var mock = new Mock<IAuthProvider>();
            mock.Setup(p => p.Authenticate("bad user", "bad pass")).Returns(false);

            var loginViewModel = new LoginViewModel
            {
                Username = "******",
                Password = "******"
            };

            var controller = new AccountController(mock.Object);
            var result = controller.Login(loginViewModel, "my-url");

            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.IsFalse(((ViewResult)result).ViewData.ModelState.IsValid);
        }
Beispiel #14
0
 public ActionResult Login(LoginViewModel model, string returnUrl)
 {
     if (ModelState.IsValid)
     {
         if (authprovider.Authenticate(model.UserName, model.Password))
         {
             return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
         }
         else
         {
             TempData["errosmessage"] = "Incorrect login or password !!!";
             return View();
         }
     }
     else
     {
         return View();
     }
 }
Beispiel #15
0
        public void Cannot_Login_With_Invalid_Credentials()
        {
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate(It.IsAny<string>(), It.IsAny<string>())).Returns<string, string>((u, p) =>
            {
                return u == "admin" && p == "secret";
            });

            LoginViewModel model = new LoginViewModel()
            {
                UserName = "******",
                Password = "******"
            };

            AccountController target = new AccountController(mock.Object);

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

            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.IsNotNull(((ViewResult)result).TempData["errosmessage"]);
        }
 public ActionResult Login(LoginViewModel model, string returnUrl)
 {
     if (ModelState.IsValid)
     {
         if (authProvider.Authenticate(model.UserName, model.Password))
         {
             // ?? called the null-coalescing operator, returns the left operand
             // if it is not null, otherwise returns the right operand
             return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
         }
         else
         {
             ModelState.AddModelError("", "Incorrect user name or password");
             return View();
         }
     }
     else
     {
         return View();
     }
 }
Beispiel #17
0
        public void Can_Login_With_Valid_Credentials()
        {
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate(It.IsAny<string>(), It.IsAny<string>())).Returns<string, string>((u, p) =>
            {
                return u == "admin" && p == "secret";
            });

            LoginViewModel model = new LoginViewModel()
            {
                UserName = "******",
                Password = "******"
            };

            AccountController target = new AccountController(mock.Object);

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

            Assert.IsInstanceOfType(result, typeof(RedirectResult));
            Assert.AreEqual("/MyURL", ((RedirectResult)result).Url);
        }
        public void Cannot_Login_With_Invalid_Credentials()
        {
            // przygotowanie - utworzenie imitacji dostawcy uwierzytelniania
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate("badUser", "badPass")).Returns(false);

            // przygotowanie - utworzenie modelu widoku
            LoginViewModel model = new LoginViewModel {
                UserName = "******",
                Password = "******"
            };

            // przygotowanie - utworzenie kontrolera
            AccountController target = new AccountController(mock.Object);

            // działanie - uwierzytelnienie z użyciem prawidłowych danych
            ActionResult result = target.Login(model, "/MyURL");

            // asercje
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.IsFalse(((ViewResult)result).ViewData.ModelState.IsValid);
        }
        public void Can_Login_With_Valid_Credentials()
        {
            // przygotowanie - utworzenie imitacji dostawcy uwierzytelniania
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate("admin", "sekret")).Returns(true);

            // przygotowanie - utworzenie modelu widoku
            LoginViewModel model = new LoginViewModel {
                UserName = "******",
                Password = "******"
            };

            // przygotowanie - utworzenie kontrolera
            AccountController target = new AccountController(mock.Object);

            // działanie - uwierzytelnienie z użyciem prawidłowych danych
            ActionResult result = target.Login(model, "/MyURL");

            // asercje
            Assert.IsInstanceOfType(result, typeof(RedirectResult));
            Assert.AreEqual("/MyURL", ((RedirectResult)result).Url);
        }
        public void Can_Login_With_Valid_Credentials()
        {
            // Arrange - Mock 인증 공급자 생성
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();
            mock.Setup(m => m.Authenticate("admin", "secret")).Returns(true);

            // Arrange - 뷰 모델을 생성
            LoginViewModel model = new LoginViewModel
            {
                UserName = "******",
                Password = "******"
            };

            // Arrange - 컨트롤러를 생서
            AccountController target = new AccountController(mock.Object);

            // Act - 유효한 자격 증명을 사용하여 인증한다.
            ActionResult result = target.Login(model, "/MyURL");

            // Assert
            Assert.IsInstanceOfType(result, typeof(RedirectResult));
            Assert.AreEqual("/MyURL", ((RedirectResult)result).Url);
        }
Beispiel #21
0
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            // Сбои при входе не приводят к блокированию учетной записи
            // Чтобы ошибки при вводе пароля инициировали блокирование учетной записи, замените на shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
            switch (result)
            {
                case SignInStatus.Success:
                    return RedirectToLocal(returnUrl);
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError("", "Неудачная попытка входа.");
                    return View(model);
            }
        }
Beispiel #22
0
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
            switch (result)
            {
                case SignInStatus.Success:
                    return RedirectToLocal(returnUrl);
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError("", "Invalid login attempt.");
                    return View(model);
            }
        }
Beispiel #23
0
        public void Can_Login_With_Valid_Credentials()
        {
            // arrange set up mock auth provider
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();

            mock.Setup(m => m.Authenticate("admin", "secret")).Returns(true);

            // create the view model
            LoginViewModel model = new LoginViewModel {UserName = "******", Password = "******"};

            // set up controller
            AccountController controller = new AccountController(mock.Object);

            // act try to login
            ActionResult result = controller.Login(model, "/MyUrl");

            // assert
            Assert.IsInstanceOfType(result, typeof(RedirectResult));
            Assert.AreEqual("/MyUrl", ((RedirectResult)result).Url);
        }
Beispiel #24
0
        public void Cant_Login_With_InValid_Credentials()
        {
            Mock<IAuthProvider> mock = new Mock<IAuthProvider>();

            mock.Setup(m => m.Authenticate("test", "test")).Returns(false);

            // create the model
            LoginViewModel model = new LoginViewModel(){UserName = "******", Password = "******"};

            AccountController controller = new AccountController(mock.Object);

            var result = controller.Login(model, "/MyUrl");

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