Exemplo 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);
        }
Exemplo n.º 2
0
        public ActionResult SetValidateEmail()
        {
            string emailcode = Request["emailcode"];
            bool   state     = true;
            string message   = string.Empty;

            try
            {
                state = UserService.CheckEamilValidate(emailcode, sessionModel.User.UserId,
                                                       sessionModel.EmailValidateCode);
                if (state)
                {
                    UserService.ChangeEmailValidateStatus(sessionModel.User.UserId, EmailValide.Valide);
                }
                sessionModel.EmailValidateCode = string.Empty;
                CacheResolver.SetCache(SessionId.GetSessionId(), sessionModel);
                message = state ? "邮箱验证成功" : "邮箱验证失败";
            }
            catch (EmailValideCodeException e)
            {
                state   = false;
                message = e.Message;
            }
            return(Content(JsonString.GetString(new { state = state, message = message })));
        }
Exemplo n.º 3
0
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            string       key   = Request["sessionid"];
            SessionModel model = null;
            bool         state = false;

            try
            {
                model = CacheResolver.GetCache(key) as SessionModel;
                if (null != model)
                {
                    state = true;
                }
            }
            catch (ArgumentNullException)
            {
                //没有缓存键,第一次登陆这个网站
                key   = Guid.NewGuid().ToString("N");
                model = new SessionModel();
            }
            catch (NullReferenceException)
            {
                model = new SessionModel();
            }
            if (!state)
            {
                //开始重新写入
                CacheResolver.SetCache(key, model);
                //开始吧key写入到cookie中去
                HttpContext.Current.Response.Cookies.Add(new HttpCookie("sessionid", key));
            }
        }
Exemplo n.º 4
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"));
        }
Exemplo n.º 5
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) })));
        }
Exemplo n.º 6
0
 public void SetCurrentUser(string sessionid, SessionModel sessionModel)
 {
     if (string.IsNullOrEmpty(sessionid) || null == sessionModel)
     {
         throw new ArgumentNullException("用户SessionId或者用户对象为空", innerException: null);
     }
     try
     {
         CacheResolver.SetCache(sessionid, sessionModel);
     }
     catch (CacheException)
     {
         //写入用户状态失败了
         throw new UserException("用户信息写入失败");
     }
 }
Exemplo n.º 7
0
        public SessionModel GetCurrentUser(string sessionid)
        {
            if (string.IsNullOrEmpty(sessionid))
            {
                throw new ArgumentNullException("用户SessionId为空", innerException: null);
            }
            SessionModel sessionModel = null;

            try
            {
                sessionModel = CacheResolver.GetCache(sessionid) as SessionModel;
            }
            catch (NullReferenceException)
            {
                throw new UserException("用户信息已经失效,请重新登陆");
            }
            return(sessionModel);
        }
Exemplo n.º 8
0
        /// <summary>
        /// De-serialize an array into a list of the correct type/
        /// </summary>
        /// <param name="value">The json list to retrieve the values from.</param>
        /// <param name="type">The type of the returned object.</param>
        /// <param name="obj"></param>
        public virtual void DeserializeArray(IList <object> value, Type type, out object obj)
        {
            obj = null;
            JsonArray list       = value as JsonArray;
            IList     resultList = null;

            if (type.IsArray)
            {
                resultList = (IList)Activator.CreateInstance(type, new object[] { list.Count });
                for (int i = 0; i < list.Count; i++)
                {
                    resultList[i] = this.DeserializeObject(resultList[i], type.GetElementType());
                }
            }
            else
            {
                if (ReflectionUtils.IsTypeGenericeCollectionInterface(type) || typeof(IList).IsAssignableFrom(type))
                {
                    Type listType = type.GetGenericArguments()[0];
                    resultList = (IList)CacheResolver.GetNewInstance(type);
                    for (int i = 0; i < list.Count; i++)
                    {
                        object listValue = list[i];

                        if (listValue != null && listValue is JsonObject)
                        {
                            JsonObject dataObj = (JsonObject)listValue;
                            if (dataObj.ContainsKey("@type"))
                            {
                                Type objType = Type.GetType((string)dataObj["@type"]);
                                resultList.Add(this.DeserializeObject(listValue, objType));
                                continue;
                            }
                        }

                        resultList.Add(this.DeserializeObject(listValue, listType));
                    }
                }

                obj = resultList;
            }
        }
Exemplo n.º 9
0
        public ActionResult ValidateEmail()
        {
            bool state = UserService.IsEmailValidated(sessionModel.User.UserId);

            if (state)
            {
                return(Content("邮箱已经被验证了"));
            }
            //开始邮箱验证工作,首先产生验证码,然后存入SessionModel中去
            ValidateCode codeobj   = new ValidateCode();
            string       emailcode = codeobj.CreateValidateCode(6);

            //更改缓存对象,同时我们数据库也要进行一次更改,用户完成了验证之后,记得要进行删除
            sessionModel.EmailValidateCode = emailcode;
            //缓存服务器保存
            CacheResolver.SetCache(SessionId.GetSessionId(), sessionModel);
            UserService.SetUserEmailValidateCode(sessionModel.User.UserId, emailcode);
            //设置完数据库之后,我们就要进行一次邮件的发送了
            UserService.SendValidateEmail(UserService.GetUserEmail(sessionModel.User.UserId), emailcode);
            return(Content("验证邮件已经发送,注意接收"));
        }
Exemplo n.º 10
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string       sessionid    = HttpContext.Current.Request["sessionid"];
            SessionModel sessionModel = null;
            bool         state        = false;

            try
            {
                sessionModel = CacheResolver.GetCache(sessionid) as SessionModel;
            }
            catch (ArgumentNullException)
            {
                //第一次登陆
                state = true;
            }
            catch (NullReferenceException)
            {
                //缓存数据失效
                state = true;
            }
            if (null == sessionModel.User)
            {
                state = true;
            }
            if (state)
            {
                //跳转登录页面
                filterContext.HttpContext.Response.Redirect("/Home/Index", true);
            }
            BaseController ba = filterContext.Controller as BaseController;

            if (null == ba)
            {
                throw new UserException("使用UserSimp属性必须要求控制器继承自BaseController");
            }
            ba.sessionModel = sessionModel;
            base.OnActionExecuting(filterContext);
        }
Exemplo n.º 11
0
 public JsonSerializer()
 {
     SetOptions(EncodeOptions.All);
     _cacheResolver = new CacheResolver(new CacheResolver.MemberMapLoader(BuildMap));
 }
Exemplo n.º 12
0
        /// <summary>
        /// De-serialize a dictionary into a dictionary of the correct type or into an object
        /// </summary>
        /// <param name="dict">The json dictionary to retrieve the values from.</param>
        /// <param name="type">The type of the returned object.</param>
        /// <param name="obj">The object that is returned.</param>
        public virtual void DeserializeDictionary(IDictionary <string, object> dict, Type type, out object obj)
        {
            obj = null;
            if (ReflectionUtils.IsTypeDictionary(type))
            {
                Type        keyType     = type.GetGenericArguments()[0];
                Type        valueType   = type.GetGenericArguments()[1];
                IDictionary dictionary2 = (IDictionary)CacheResolver.GetNewInstance(type);
                foreach (KeyValuePair <string, object> current in dict)
                {
                    Type   objType = valueType;
                    object data    = current.Value;

                    if (current.Value != null && current.Value.GetType() == typeof(JsonObject))
                    {
                        JsonObject dataObj = (JsonObject)current.Value;
                        if (dataObj.ContainsKey("@type"))
                        {
                            objType = Type.GetType((string)dataObj["@type"]);
                        }
                    }

                    dictionary2.Add(current.Key, this.DeserializeObject(data, objType));
                }
                obj = dictionary2;
            }
            else if (TryDeserializeCustomType(type, dict, out obj))
            {
                // The function should handle any conversions that are required.
            }
            else
            {
                obj = CacheResolver.GetNewInstance(type);
                Dictionary <string, CacheResolver.MemberMap> memberDict = _cacheResolver.LoadMaps(type);
                if (memberDict == null)
                {
                    obj = dict;
                }
                else
                {
                    foreach (KeyValuePair <string, CacheResolver.MemberMap> objMember in memberDict)
                    {
                        CacheResolver.MemberMap memberVal = objMember.Value;
                        if (memberVal.Setter != null)
                        {
                            string key = objMember.Key;
                            if (dict.ContainsKey(key))
                            {
                                Type   objType = memberVal.Type;
                                object data    = dict[key];

                                if (data != null && data is JsonObject)
                                {
                                    JsonObject dataObj = (JsonObject)data;
                                    if (dataObj.ContainsKey("@type"))
                                    {
                                        objType = Type.GetType((string)dataObj["@type"]);
                                    }
                                }

                                object memberResult = this.DeserializeObject(data, objType);
                                memberVal.Setter(obj, memberResult);
                            }
                        }
                    }

                    if (typeof(IJsonSerializable).IsAssignableFrom(type))
                    {
                        // TODO:
                        // obj
                    }
                }
            }
        }
Exemplo n.º 13
0
        public virtual object deserializeObject(object value, Type type)
        {
            object result = null;

            if (value is string)
            {
                string text = value as string;
                result = ((string.IsNullOrEmpty(text) || (type != typeof(DateTime) && (!ReflectionUtils.isNullableType(type) || Nullable.GetUnderlyingType(type) != typeof(DateTime)))) ? text : ((object)DateTime.ParseExact(text, Iso8601Format, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal)));
            }
            else if (value is bool)
            {
                result = value;
            }
            else if (value == null)
            {
                result = null;
            }
            else if ((value is long && type == typeof(long)) || (value is double && type == typeof(double)))
            {
                result = value;
            }
            else
            {
                if ((!(value is double) || type == typeof(double)) && (!(value is long) || type == typeof(long)))
                {
                    if (value is IDictionary <string, object> )
                    {
                        IDictionary <string, object> dictionary = (IDictionary <string, object>)value;
                        if (ReflectionUtils.isTypeDictionary(type))
                        {
                            Type        type2       = type.GetGenericArguments()[0];
                            Type        type3       = type.GetGenericArguments()[1];
                            Type        type4       = typeof(Dictionary <, >).MakeGenericType(type2, type3);
                            IDictionary dictionary2 = (IDictionary)CacheResolver.getNewInstance(type4);
                            foreach (KeyValuePair <string, object> item in dictionary)
                            {
                                dictionary2.Add(item.Key, deserializeObject(item.Value, type3));
                            }
                            result = dictionary2;
                        }
                        else
                        {
                            result = CacheResolver.getNewInstance(type);
                            SafeDictionary <string, CacheResolver.MemberMap> safeDictionary = cacheResolver.loadMaps(type);
                            if (safeDictionary != null)
                            {
                                {
                                    foreach (KeyValuePair <string, CacheResolver.MemberMap> item2 in safeDictionary)
                                    {
                                        CacheResolver.MemberMap value2 = item2.Value;
                                        if (value2.Setter != null)
                                        {
                                            string key = item2.Key;
                                            if (dictionary.ContainsKey(key))
                                            {
                                                object value3 = deserializeObject(dictionary[key], value2.Type);
                                                value2.Setter(result, value3);
                                            }
                                        }
                                    }
                                    return(result);
                                }
                            }
                            result = value;
                        }
                    }
                    else if (value is IList <object> )
                    {
                        IList <object> list  = (IList <object>)value;
                        IList          list2 = null;
                        if (type.IsArray)
                        {
                            list2 = (IList)Activator.CreateInstance(type, list.Count);
                            int num = 0;
                            foreach (object item3 in list)
                            {
                                list2[num++] = deserializeObject(item3, type.GetElementType());
                            }
                        }
                        else if (ReflectionUtils.isTypeGenericeCollectionInterface(type) || typeof(IList).IsAssignableFrom(type))
                        {
                            Type type5 = type.GetGenericArguments()[0];
                            Type type6 = typeof(List <>).MakeGenericType(type5);
                            list2 = (IList)CacheResolver.getNewInstance(type6);
                            foreach (object item4 in list)
                            {
                                list2.Add(deserializeObject(item4, type5));
                            }
                        }
                        result = list2;
                    }
                    return(result);
                }
                result = ((value is long && type == typeof(DateTime)) ? ((object)new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds((long)value)) : ((!type.IsEnum) ? ((!typeof(IConvertible).IsAssignableFrom(type)) ? value : Convert.ChangeType(value, type, CultureInfo.InvariantCulture)) : Enum.ToObject(type, value)));
            }
            if (ReflectionUtils.isNullableType(type))
            {
                return(ReflectionUtils.toNullableType(result, type));
            }
            return(result);
        }
Exemplo n.º 14
0
 public PocoJsonSerializerStrategy()
 {
     cacheResolver = new CacheResolver(buildMap);
 }