码迷,mamicode.com
首页 > Web开发 > 详细

.net Cookie操作公共方法 CookieHelper

时间:2019-12-30 19:05:00      阅读:96      评论:0      收藏:0      [点我收藏+]

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

Cookie操作类

/// <summary>
    /// Cookie帮助类
    /// </summary>
    public class CookieHelper
    {
        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="cookieName">名称</param>
        /// <param name="cookieValue"></param>
        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);
        }
        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="cookieName">名称</param>
        /// <param name="cookieValue"></param>
        /// <param name="expires">过期时间(分钟)</param>
        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);
        }
        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="cookieName">名称</param>
        /// <returns>cookie值</returns>
        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 "";
        }
        /// <summary>
        /// 删除Cookie对象
        /// </summary>
        /// <param name="cookieName">Cookie对象名称</param>
        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

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