CookieHelper

Posted HOT SUMMER

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CookieHelper相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Common
{
    public class CookieHelper
    {
        /// <summary>
        /// 保存一个Cookie
        /// </summary>
        /// <param name="CookieName">Cookie名称</param>
        /// <param name="CookieValue">Cookie值</param>
        /// <param name="CookieTime">Cookie过期时间(小时),0为关闭页面失效</param>
        public static void SaveCookie(string CookieName, string CookieValue, double CookieTime)
        {
            HttpCookie myCookie = new HttpCookie(CookieName);
            DateTime now = DateTime.Now;
            myCookie.Value = CookieValue;

            if (CookieTime != 0)
            {
                myCookie.Expires = now.AddDays(CookieTime);
                if (HttpContext.Current.Response.Cookies[CookieName] != null)
                    HttpContext.Current.Response.Cookies.Remove(CookieName);

                HttpContext.Current.Response.Cookies.Add(myCookie);
            }
            else
            {
                if (HttpContext.Current.Response.Cookies[CookieName] != null)
                    HttpContext.Current.Response.Cookies.Remove(CookieName);

                HttpContext.Current.Response.Cookies.Add(myCookie);
            }
        }


        /// <summary>
        /// 取得CookieValue
        /// </summary>
        /// <param name="CookieName">Cookie名称</param>
        /// <returns>Cookie的值</returns>
        public static string GetCookie(string CookieName)
        {
            HttpCookie myCookie = new HttpCookie(CookieName);
            myCookie = HttpContext.Current.Request.Cookies[CookieName];

            if (myCookie != null)
                return myCookie.Value;
            else
                return null;
        }


        /// <summary>
        /// 清除CookieValue
        /// </summary>
        /// <param name="CookieName">Cookie名称</param>
        public static void ClearCookie(string CookieName)
        {
            HttpCookie myCookie = new HttpCookie(CookieName);
            DateTime now = DateTime.Now;

            myCookie.Expires = now.AddYears(-2);

            HttpContext.Current.Response.Cookies.Add(myCookie);
        }



        #region 清除Cookie

        /// <summary>
        /// 清除Cookie
        /// </summary>
        /// <param name="cookieName"></param>
        public static void ClearCookie(string cookieName, string domain, string path)
        {
            cookieName = Encode(cookieName);

            HttpCookie cookie = new HttpCookie(cookieName);
            cookie.Domain = domain;
            cookie.Path = path;
            cookie.Expires = DateTime.Now.AddDays(-360);

            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 从多值Cookie移除个subKey
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="subKey"></param>
        public static void ClearCookie(string cookieName, string subKey, string domain, string path)
        {
            cookieName = Encode(cookieName);
            subKey = Encode(subKey);

            HttpCookie cookie;

            if (System.Web.HttpContext.Current.Request.Cookies[cookieName] == null)
            {
                cookie = new HttpCookie(cookieName);
            }
            else
            {
                cookie = System.Web.HttpContext.Current.Request.Cookies[cookieName];
            }
            cookie.Domain = domain;
            cookie.Path = path;

            cookie.Values.Remove(subKey);

            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static string Encode(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return null;
            }
            // return AntiXssEncoderTwo.UrlEncode(input);
            return HttpUtility.UrlEncode(input);//.Replace("_", "%5F").Replace("&", "%26").Replace("+", "%2B");
        }

        public static string Decode(string input)
        {
            return HttpUtility.UrlDecode(input);
        }
        #endregion   
    }
}

 

以上是关于CookieHelper的主要内容,如果未能解决你的问题,请参考以下文章

CookieHelper.cs(20170223)

C# 常用开发类库整理

Dos.Common

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js