Ejemplo n.º 1
0
        public static string Get(string name, bool isEncryp = false)
        {
            CookieEntity entity = new CookieEntity(name);

            entity.IsEncryp = isEncryp;
            return(Get(entity));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 根据CookieName 获取Cookie
        /// </summary>
        /// <param name="CookieName">cookie名称</param>
        /// <returns></returns>
        public static T Get <T>(CookieEntity <T> entity) where T : class
        {
            string cookieName = entity.CookieName;
            var    cookies    = HttpContext.Current.Request.Cookies;
            string value      = string.Empty;

            if (cookies != null && cookies[cookieName] != null)
            {
                value = cookies[cookieName].Value;
                if (!string.IsNullOrEmpty(value))
                {
                    if (entity.IsEncode)
                    {
                        value = HttpUtility.UrlDecode(value, entity.Encoding);
                    }
                    if (entity.IsEncryp)
                    {
                        value = EncryptHelper.Decrypt(entity.EncrypKey, value);
                    }
                }
            }
            if (string.IsNullOrEmpty(value))
            {
                return(default(T));
            }
            return(JsonHelper.ToObject <T>(value));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 根据CookieName 获取Cookie
        /// </summary>
        /// <param name="CookieName">cookie名称</param>
        /// <returns></returns>
        public static string Get(CookieEntity entity)
        {
            string cookieName = entity.CookieName;
            var    cookies    = HttpContext.Current.Request.Cookies;

            if (cookies != null && cookies[cookieName] != null)
            {
                string value = cookies[cookieName].Value;
                if (string.IsNullOrEmpty(value))
                {
                    return(string.Empty);
                }
                if (entity.IsEncode)
                {
                    value = HttpUtility.UrlDecode(value, entity.Encoding); //2013-11-08修改
                }
                if (entity.IsEncryp)
                {
                    return(EncryptHelper.Decrypt(entity.EncrypKey, value));
                }
                else
                {
                    return(value);
                }
            }
            return(string.Empty);
        }
Ejemplo n.º 4
0
        public static T Get <T>(string name, bool isEncryp = false) where T : class
        {
            CookieEntity <T> entity = new CookieEntity <T>(name);

            entity.IsEncryp = isEncryp;
            return(Get <T>(entity));
        }
Ejemplo n.º 5
0
        public static void Add <T>(string name, T value, DateTimeType type = DateTimeType.Day, int number = 1) where T : class
        {
            CookieEntity <T> entity = new CookieEntity <T>(name, value);

            entity.Type   = type;
            entity.Number = number;
            Add <T>(entity);
        }
Ejemplo n.º 6
0
        public static void Add(string name, string value, DateTimeType type = DateTimeType.Day, int number = 1, bool encryp = false)
        {
            CookieEntity entity = new CookieEntity(name, value);

            entity.Type     = type;
            entity.Number   = number;
            entity.IsEncryp = encryp;
            Add(entity);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 添加Cookie
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        public static void Add <T>(CookieEntity <T> entity) where T : class
        {
            HttpCookie cookie = new HttpCookie(entity.CookieName);
            string     v      = typeof(T) == typeof(string) ? entity.Value.ToString() : JsonHelper.ToJson(entity.Value);

            if (entity.IsEncryp)
            {
                v = EncryptHelper.Encrypt(entity.EncrypKey, v);
            }
            if (entity.IsEncode)
            {
                v = HttpUtility.UrlEncode(v, entity.Encoding);
            }
            cookie.Value   = v;
            cookie.Path    = entity.Path;
            cookie.Domain  = entity.Domain;
            cookie.Expires = DateTimeTypeHelper.GetDateTime(entity.Type, entity.Number);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// 添加Cookie
 /// </summary>
 /// <param name="entity"></param>
 public static void Add(CookieEntity entity)
 {
     Add <string>(entity);
 }