Exemplo n.º 1
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"));
         }
         ModelState.AddModelError("", "Incorrect username or password");
         return View();
     }
     return View();
 }
Exemplo n.º 2
0
        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);
            var result = target.Login(model, "/MyURL");

            Assert.IsInstanceOfType(result, typeof(RedirectResult));
            Assert.AreEqual("/MyURL", ((RedirectResult)result).Url);
        }
Exemplo n.º 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 LoginViewModel
            {
                UserName = "******",
                Password = "******"
            };

            AccountController target = new AccountController(mock.Object);
            var result = target.Login(model, "/MyURL");

            Assert.IsInstanceOfType(result, typeof(ViewResult));
            Assert.IsFalse(((ViewResult)result).ViewData.ModelState.IsValid);
        }
Exemplo n.º 4
0
 public ActionResult Login(LoginViewModel model, string returnUrl)
 {
     if (ModelState.IsValid)
     {
         if (authProvider.Authenticate(model.UserName, model.Password))
         {
             FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: true);
             return Redirect(returnUrl ?? Url.Action("Index", "Admin"));
         }
         else
         {
             ModelState.AddModelError("", "Неправильные имя пользователя или пароль");
             return View();
         }
     }
     else
     {
         return View();
     }
 }
Exemplo n.º 5
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
         {
             ModelState.AddModelError("", "Błędny login lub hasło");
       return View();
         }
     }
     else
     {
         return View();
     }
 }
 public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
 {
     if (ModelState.IsValid)
     {
         var user = UserManager.FindByName(model.UserName);
         if (user != null)
         {
             if (user.EmailConfirmed)
             {
                 // Сбои при входе не приводят к блокированию учетной записи
                 // Чтобы ошибки при вводе пароля инициировали блокирование учетной записи, замените на shouldLockout: true
                 var result = await SignInManager.PasswordSignInAsync(model.UserName, 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);
                 }
             }
             else
             {
                 ModelState.AddModelError("", "Не подтвержден email.");
             }
         }
         else
         {
             ModelState.AddModelError("", "Неудачная попытка входа.");
         }
     }
     return View(model);
 }
        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);
            }
        }