zoukankan      html  css  js  c++  java
  • Cookie操作公共方法类

    using System;
    using System.Web;
    
    namespace AmwITx.Framework.Web.Components
    {
        /// <summary>
        /// Cookie
        /// </summary>
        public class Cookie
        {
            private Cookie() { }
    
            private static Cookie _instance = new Cookie();
    
            public static Cookie Instance
            {
                get { return _instance; }
                set { _instance = value; }
            }
    
            /// <summary>
            /// SetCookie
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            public void Set(string key, string value)
            {
                Set(key, value, "/", "", 0);
            }
    
            /// <summary>
            /// SetCookie
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <param name="path"></param>
            /// <param name="domain">二级域名时定位</param>
            /// <param name="hour"></param>
            public void Set(string key, string value, string path, string domain, int hour)
            {
                Set(key, value, path, domain, DateTime.Now.AddHours(hour));
            }
    
            /// <summary>
            /// SetCookie
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <param name="path"></param>
            /// <param name="domain">二级域名时定位</param>
            /// <param name="expires"></param>
            public void Set(string key, string value, string path, string domain, DateTime expires)
            {
                var cookie = HttpContext.Current.Request.Cookies[key];
                bool isAdd = false;
                if (cookie == null)
                {
                    isAdd = true;
                    cookie = new HttpCookie(key);
                }
                cookie.Path = path;
                cookie.Domain = domain;
                cookie.Value = HttpUtility.UrlEncode(value);
                if (expires > DateTime.Now)
                {
                    cookie.Expires = expires;
                }
    
                if (isAdd)
                {
                    HttpContext.Current.Response.Cookies.Add(cookie);
                }
                else
                {
                    HttpContext.Current.Response.Cookies.Set(cookie);
                }
            }
    
            /// <summary>
            /// Cookie
            /// </summary>
            public string Get(string key)
            {
                var cookie = HttpContext.Current.Request.Cookies[key];
                return cookie != null ? HttpUtility.UrlDecode(cookie.Value) : "";
            }
            /// <summary>
            /// Cookie
            /// </summary>
            /// <param name="key"></param>
            public void Remove(string key)
            {
                Remove(key, "", "");
            }
    
            public void Remove(string key, string path, string domain)
            {
                var hc = new HttpCookie(key);
                if (path != "")
                {
                    hc.Path = path;
                }
                if (domain != "")
                {
                    hc.Domain = domain;
                }
                hc.Expires = DateTime.Now.AddDays(-1);
    
                HttpContext.Current.Response.Cookies.Add(hc);
            }
        }
    }
  • 相关阅读:
    js中图片base64格式转文件对象
    vue中添加百度统计代码
    一个基于vueCli3的移动端多页面脚手架
    一个基于WabPack4的 Vue 多页面原生写法脚手架
    GPU加速有坑?
    mac效率工具
    git操作详解
    高性能移动端开发
    浅析vue数据绑定
    移动端适配
  • 原文地址:https://www.cnblogs.com/tearer/p/1778225.html
Copyright © 2011-2022 走看看