Beispiel #1
0
        public ActionResult CheckLogin(string username, string password, string code)
        {
            try
            {
                if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
                {
                    return(Content(new AjaxResult {
                        state = ResultType.error, message = "请输入用户名和密码!"
                    }.ToJson()));
                }

                if (Session["session_verifycode"] == null || VerifyCode.md5(code.ToLower(), 16) != Session["session_verifycode"].ToString())
                {
                    WriteLog(ResultType.error.ToString(), $"验证码错误");
                    return(Content(new AjaxResult {
                        state = ResultType.error, message = "验证码错误,请重新输入!"
                    }.ToJson()));
                }
                UserInfo.Instance.Account = username.Trim();
                var resultEntity = UserBLL.Instance.GetUserByLogin(UserInfo.Instance);
                if (resultEntity != null)
                {
                    var encrytPass = Encodetool.Md5(password.Trim());
                    if (encrytPass != resultEntity.Password)
                    {
                        WriteLog(ResultType.error.ToString(), "用户密码错误");
                        return(Content(new AjaxResult {
                            state = ResultType.error, message = "用户密码错误!"
                        }.ToJson()));
                    }
                    UserInfo.Instance           = resultEntity;
                    UserInfo.Instance.UserRoles = SysBLL.Instance.GetUserRoles(resultEntity.ID);
                    WebHelper.WriteCookie("loginTag", Encodetool.Encrypt(UserInfo.Instance.ToJson()));
                    WriteLog(ResultType.success.ToString(), "登录成功");
                }
                else
                {
                    return(Content(new AjaxResult {
                        state = ResultType.error.ToString(), message = "用户名或密码不正确!"
                    }.ToJson()));
                }
                return(Content(new AjaxResult {
                    state = ResultType.success.ToString(), message = "登录成功!"
                }.ToJson()));
            }
            catch (Exception ex)
            {
                WriteLog(ResultType.success.ToString(), $"登录失败!失败原因:{ex.Message}");
                return(Content(new AjaxResult {
                    state = ResultType.error.ToString(), message = ex.Message
                }.ToJson()));
            }
        }