码迷,mamicode.com
首页 > 其他好文 > 详细

基本类-CookieStorage

时间:2014-11-05 12:18:29      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:blog   http   ar   os   sp   div   on   log   ad   

简单建立Cookie:

public enum CookieKey
    {
        UserLogin
    }

    public class CookieStorage
    {
        private const int LOGINEXPIRESTIME = 24 * 60;
        public static object SetCookie(CookieKey key, string value)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key.ToString()];
            if (cookie == null)
            {
                cookie = new HttpCookie(key.ToString());
                string result = HttpUtility.UrlEncode(value);
                cookie.Value = result;
                cookie.Expires = System.DateTime.Now.AddMinutes(LOGINEXPIRESTIME);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }

            return value;
        }

        public static string GetCookie(CookieKey key)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key.ToString()];
            if (cookie != null)
            {
                string result = HttpUtility.UrlDecode(cookie.Value);
                return result;
            }

            return string.Empty;
        }

        public static void ClearCookie(CookieKey key) { 
             HttpCookie cookie = HttpContext.Current.Request.Cookies[key.ToString()];
             if (cookie != null)
             {
                 cookie.Expires = DateTime.Now.AddDays(-1);
                 HttpContext.Current.Response.Cookies.Add(cookie);
             }
        }
    }

  

基本类-CookieStorage

标签:blog   http   ar   os   sp   div   on   log   ad   

原文地址:http://www.cnblogs.com/xiguain/p/4075698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!