Example #1
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.UsernameOrEmail, model.Password, true, 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:
                    var user = UserManager.Users.FirstOrDefault(u => u.Email == model.UsernameOrEmail);
                    if (user != null)
                    {
                        if (UserManager.CheckPassword(user, model.Password))
                        {
                            SignInManager.SignIn(user, false, true);
                            return RedirectToLocal(returnUrl);
                        }
                    }

                    ModelState.AddModelError("", "Nope, try again.");
                    return View(model);
            }
        }
Example #2
0
        public async Task<ActionResult> LoginModal(LoginViewModel model)
        {

            if (!ModelState.IsValid)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Content("Login Unsuccessful", MediaTypeNames.Text.Plain);
            }

            // 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.UsernameOrEmail , model.Password, model.RememberMe, shouldLockout: false);

            switch (result)
            {
                case SignInStatus.Success:
                    Response.StatusCode = (int)HttpStatusCode.OK;
                    return Content("OK", MediaTypeNames.Text.Plain);
                    
                case SignInStatus.LockedOut:
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return Content("Account Locked Out", MediaTypeNames.Text.Plain);
                case SignInStatus.RequiresVerification:
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return Content("Account Requires Verification", MediaTypeNames.Text.Plain);
                case SignInStatus.Failure:
                default:
                    var user = UserManager.Users.FirstOrDefault(u => u.Email  == model.UsernameOrEmail);
                    if (user != null)
                    {
                        if (UserManager.CheckPassword(user, model.Password ))
                        {
                            SignInManager.SignIn(user, false , true);
                            Response.StatusCode = (int)HttpStatusCode.OK;
                            return Content("OK", MediaTypeNames.Text.Plain);
                        }
                    }

                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return Content("Login Unsuccessful", MediaTypeNames.Text.Plain);
            }
        }