예제 #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 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));
        }
        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));
        }