Ejemplo n.º 1
0
        /*
         * ************控制器私有方法************
         */
        private bool ValidateUserCode(string code)
        {
            //先获取服务器端的code
            if (string.IsNullOrEmpty(code))
            {
                return(false);
            }
            string servercode = String.Empty;

            try
            {
                servercode = CacheResolver.GetCache(ValidateCodeId.GetValidateCodeId()) as string;
            }
            catch (NullReferenceException)
            {
                //缓存的数据失效了
                return(false);
            }
            if (null == servercode)
            {
                return(false);
            }
            if (code != servercode)
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 2
0
        public ActionResult GetValidateCode()
        {
            //有可能会用户刷新验证码,所以我们要消除原来的验证码
            CacheResolver.DeleteCache(ValidateCodeId.GetValidateCodeId());
            ValidateCode code      = new ValidateCode();
            string       codevalue = code.CreateValidateCode(6);//6位长度的值

            //开始存入缓存服务器
            CacheResolver.SetCache(ValidateCodeId.GetValidateCodeId(), codevalue);
            return(File(code.CreateValidateGraphic(codevalue), @"image/jpeg"));
        }
Ejemplo n.º 3
0
        public ActionResult Login()
        {
            string message = string.Empty;
            bool   state   = false;

            if (!ValidateUserCode(Request["validatecode"]))
            {
                message = "验证码错误";
                CacheResolver.DeleteCache(ValidateCodeId.GetValidateCodeId());
                return(Content(JsonString.GetString(new { state = state, message = message })));
            }
            string       account             = Request["account"];
            string       password            = Request["password"];
            SessionModel currentSessionModel = new SessionModel();

            try
            {
                currentSessionModel.User = UserService.Login(account, password);
                state   = true;
                message = "登录成功";
            }
            catch (UserException e)
            {
                message = e.Message;
            }
            catch (ArgumentNullException e)
            {
                message = e.Message;
            }//删除验证码
            finally
            {
                CacheResolver.DeleteCache(ValidateCodeId.GetValidateCodeId());
            }
            if (state == false)
            {
                //开始返回
                return(Content(JsonString.GetString(new { state = state, message = message })));
            }
            //在请求的开始已经处理了sessionid的写入了
            UserState.SetCurrentUser(SessionId.GetSessionId(), currentSessionModel);
            //不进行捕获异常,由mvc来进行处理
            bool rem = Request["remember"] == "1";

            //其中的密码是md5密码
            RememeberMe(account, currentSessionModel.User.Password, rem);
            //登录成功要做的就是要消除这个验证码
            CacheResolver.DeleteCache(ValidateCodeId.GetValidateCodeId());
            return(Content(JsonString.GetString(new { state = state, message = message, user = UserService.GetViewUser(currentSessionModel.User) })));
        }