.net Cookie操作公共方法 CookieHelper

2021-05-03 17:30

阅读:665

标签:public   help   名称   min   操作   bsp   trim   controls   return   

Cookie操作类

/// 
    /// Cookie帮助类
    /// 
    public class CookieHelper
    {
        /// 
        /// 写cookie值
        /// 
        /// 名称
        /// 
        public static void AddCookie(string cookieName, string cookieValue)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie == null)
            {
                cookie = new HttpCookie(cookieName);
            }
            cookie.Value = cookieValue;
            HttpContext.Current.Response.AppendCookie(cookie);
        }
        /// 
        /// 写cookie值
        /// 
        /// 名称
        /// 
        /// 过期时间(分钟)
        public static void AddCookie(string cookieName, string cookieValue, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie == null)
            {
                cookie = new HttpCookie(cookieName);
            }
            cookie.Value = cookieValue;
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            HttpContext.Current.Response.AppendCookie(cookie);
        }
        /// 
        /// 读cookie值
        /// 
        /// 名称
        /// cookie值
        public static string GetCookie(string cookieName)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                return HttpContext.Current.Request.Cookies[cookieName].Value.ToString();
            }
            return "";
        }
        /// 
        /// 删除Cookie对象
        /// 
        /// Cookie对象名称
        public static void RemoveCookie(string cookieName)
        {
            HttpCookie objCookie = new HttpCookie(cookieName.Trim());
            objCookie.Expires = DateTime.Now.AddYears(-5);
            HttpContext.Current.Response.Cookies.Add(objCookie);
        }
    }

 

.net Cookie操作公共方法 CookieHelper

标签:public   help   名称   min   操作   bsp   trim   controls   return   

原文地址:https://www.cnblogs.com/jiangqw/p/12121249.html


评论


亲,登录后才可以留言!