示例#1
0
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            
            // Require the user to have a confirmed email before they can log on.
            var user = await UserManager.FindByEmailAsync(model.UserName);
            if (user!=null)
            {
                
                if (!await UserManager.IsEmailConfirmedAsync(user.Id))
                {
                    string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account-Resend");
                    ViewBag.errorMessage = "you must have a confirmed email to log on." 
                        + "The confirmation token has been resent to your email account.";
                   
                    return View("Error");
                }
            }

            // 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.UserName, model.Password, model.RememberMe, shouldLockout: false);
            switch (result)
            {
                case SignInStatus.Success:
                    var applicationUser = await UserManager.FindByNameAsync(model.UserName);
                    if (applicationUser != null)
                    {
                        AspNetUser aspnetUser = new AspNetUser();
                        aspnetUser.Id = applicationUser.Id;
                        var userLogin = db.AspNetUsers.Find(aspnetUser.Id);
                        userLogin.LockoutEnabled = false;
                        db.Entry(userLogin).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                    }
                    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);
            }
            
        }
示例#2
0
        public async Task<ActionResult> LogOff(LoginViewModel model)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
            var applicationUser = await UserManager.FindByNameAsync(User.Identity.Name);
            if (applicationUser != null)
            {
                AspNetUser aspnetUser = new AspNetUser();
                aspnetUser.Id = applicationUser.Id;
                var userLogin = db.AspNetUsers.Find(aspnetUser.Id);
                userLogin.LockoutEnabled = true;
                db.Entry(userLogin).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }           

            return RedirectToAction("Index", "Home");
        }