Beispiel #1
0
 public ActionResult LogOn(LogOnModel model)
 {
     if (ModelState.IsValid)
     {
         var userPassword = _adminUserTask.GetByUserName(model.UserName);
         if (userPassword == null)
         {
             return(AlertMsg("账号不存在", Request.UrlReferrer.PathAndQuery));
         }
         if (userPassword.Password != CryptTools.HashPassword(model.Password))
         {
             return(AlertMsg("账号或密码不正确", Request.UrlReferrer.PathAndQuery));
         }
         if (userPassword.IsLock)
         {
             return(AlertMsg("对不起,您的账号被锁定", Request.UrlReferrer.PathAndQuery));
         }
         if (model.ValidationCode != context.Session["code"].ToString())
         {
             return(AlertMsg("验证码不正确", Request.UrlReferrer.PathAndQuery));
         }
         FormsAuthService.SignIn(userPassword.UserName, false);
         return(RedirectToAction("Index", "Home"));
     }
     return(View(model));
 }
 public ActionResult LogOut()
 {
     if (FormsAuthService.IsSignedIn())
     {
         FormsAuthService.SignOut();
     }
     return(RedirectToAction("LogOn", "Account"));
 }
        public ActionResult SignUp(ComboSignupLoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserNew.UserName, model.UserNew.Password, model.UserNew.Email);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    try
                    {
                        //finish the registration that the MembershipProvider did not handle.
                        User user = UserRepository.CompleteRegistration(_db, model.UserNew.UserName, model.UserNew.FirstName, model.UserNew.LastName);

                        //log that user registered.
                        _log.LogIt(user.UserId, "User registered");

                        this.FlashInfo("Thank you for signing up!");

                        FormsAuthService.SignIn(user.UserId, user.Username, false /* createPersistentCookie */);

                        //cache user data.
                        CacheHelper.CacheUserData(FormsAuthService, user);

                        return(Redirect(user, returnUrl));
                    }
                    catch (Exception exp)
                    {
                        ModelState.AddModelError("", exp.Message);
                        this.FlashValidationSummaryErrors();
                    }
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                    this.FlashValidationSummaryErrors();
                }
            }
            else
            {
                this.FlashValidationSummaryErrors();
            }

            // If we got this far, something failed, redisplay form
            return(View("login", model));
        }
Beispiel #4
0
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            var name = FormsAuthService.GetCurrentIdentity().Name;
            var user = _adminUserTask.GetByUserName(name);

            if (user.Password != CryptTools.HashPassword(model.OldPw))
            {
                ModelState.AddModelError("OldPw", "旧密码不正确");
            }
            if (model.NewPw != model.AgainPw)
            {
                ModelState.AddModelError("AgainPw", "两次密码不一致");
            }
            if (ModelState.IsValid)
            {
                user.Password = CryptTools.HashPassword(model.AgainPw);
                _adminUserTask.Update(user);
                return(AlertMsg("修改成功!", "/account/index"));
            }
            return(View());
        }
        public ActionResult Login(ComboSignupLoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserLogin.LoginName, model.UserLogin.Password))
                {
                    //get user.
                    User user = UserRepository.GetUser(_db, model.UserLogin.LoginName);
                    if (user != null)
                    {
                        //log that the user logged in.
                        _log.LogIt(user.UserId, "User Logged In");

                        FormsAuthService.SignIn(user.UserId, user.Username, model.UserLogin.RememberMe);

                        //cache user data.
                        CacheHelper.CacheUserData(FormsAuthService, user);

                        return(Redirect(user, returnUrl));
                    }
                    else
                    {
                        ModelState.AddModelError("", "User info could not be found.");
                        this.FlashValidationSummaryErrors();
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                    this.FlashValidationSummaryErrors();
                }
            }
            else
            {
                this.FlashValidationSummaryErrors();
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                //we need the username for this to work so get the user.
                User user = UserRepository.GetUser(_db, FormsAuthService.GetCurrentUserId());
                if (MembershipService.ChangePassword(user.Username, model.OldPassword, model.NewPassword))
                {
                    return(RedirectToAction("ChangePasswordSuccess"));
                }
                else
                {
                    ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                    this.FlashValidationSummaryErrors();
                }
            }
            else
            {
                this.FlashValidationSummaryErrors();
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
 //Logout
 public ActionResult Logout()
 {
     _log.LogIt(FormsAuthService.GetCurrentUserId(), "Logged out");
     FormsAuthService.SignOut();
     return(RedirectToAction("Index", "Home"));
 }