Ejemplo n.º 1
0
        public ActionResult Login(LoginModel model)
        {
            if (string.IsNullOrEmpty(model.LoginName))
            {
                ModelState.AddModelError("", "用户名不能为空");
            }
            else
            {
                model.LoginName = model.LoginName.Trim();
            }

            if (string.IsNullOrEmpty(model.Password))
            {
                ModelState.AddModelError("", "密码不能为空");
            }

            string strSignInImageCode = _workContext.GetLoginImageCode();

            if (!string.IsNullOrWhiteSpace(strSignInImageCode))
            {
                if (string.IsNullOrEmpty(model.ValidationCode))
                {
                    ModelState.AddModelError("", "验证码不能为空");
                    _authenticationService.IncreaseLoginErrorCount();
                }
                else if (!strSignInImageCode.Equals(model.ValidationCode))
                {
                    ModelState.AddModelError("", "验证码输入有误");
                    _authenticationService.IncreaseLoginErrorCount();
                }
            }

            if (ModelState.IsValid)
            {
                string strMsg;
                var    user = _userService.ValidateUser(model.LoginName, model.Password, out strMsg);
                if (user != null)
                {
                    _authenticationService.SignIn(new UserData {
                        UserName = model.LoginName
                    }, false);
                    _authenticationService.RemoveLoginErrorCount();
                    _authenticationService.RemoveLoginImageCode();
                    return(RedirectToHomePage());
                }
                ModelState.AddModelError("", "用户名或密码有误.");
                _authenticationService.IncreaseLoginErrorCount();
            }

            if (_authenticationService.GetLoginErrorCount() >= LoginModel.S_LoginAllowedErrorCount)
            {
                model.DisplayValidationCode = true;
            }
            return(View(model));
        }