• Cookie的设置


    using System;
    using System.Web;
    using XTSoft.Com.DwCmsException;
    
    namespace XTSoft.Com.Common
    {
        public sealed class CookieHelper
        {
            private CookieHelper()
            {
    
            }
    
            /// <summary>
            /// 获取一个数组形式的Cookies
            /// </summary>
            public static HttpCookie GetCookie(string name)
            {
                if (!string.IsNullOrEmpty(name))
                {
                    return HttpContext.Current.Request.Cookies[name];
                }
                else
                {
                    throw new DwException("键值为空!");
                }
            }
    
            /// <summary>
            /// 移除Cookies
            /// </summary>
            public static void RemoveCookie(string name)
            {
                RemoveCookie(GetCookie(name));
            }
    
            /// <summary>
            /// 移除Cookies
            /// </summary>
            public static void RemoveCookie(HttpCookie cookie)
            {
                if (cookie != null)
                {
                    cookie.Expires = new DateTime(1983, 1, 2);
                    Save(cookie);
                }
            }
    
            /// <summary>
            /// 保存Cookies
            /// </summary>
            public static void Save(HttpCookie cookie)
            {
    
                cookie.Domain = "";
                HttpContext.Current.Response.AppendCookie(cookie);
            }
    
            /// <summary>
            /// 获取一个新的Cookies
            /// </summary>
            public static HttpCookie GetNewCookie(string name)
            {
                return new HttpCookie(name);
            }
    
            /// <summary>
            /// 取得指定名称的单值Cookie
            /// </summary>
            /// <returns></returns>
            public static string GetCookieValue(string name)
            {
                HttpCookie cookie = GetCookie(name);
                if (cookie == null || cookie.Value == null)
                {
                    return string.Empty;
                }
                else
                {
                    return cookie.Value;
                }
            }
    
            /// <summary>
            /// 保存指定名称的单值Cookie
            /// </summary>
            /// <returns></returns>
            public static void SetCookie(string name, string value)
            {
                SetCookie(name, value, DateTime.Now);
            }
    
            /// <summary>
            /// 保存指定名称的单值Cookie
            /// </summary>
            /// <returns></returns>
            public static void SetCookie(string name, string value, DateTime expires)
            {
                HttpCookie cookie = GetCookie(name);
                if (cookie == null)
                {
                    cookie = GetNewCookie(name);
                }
                cookie.Value = value;
                cookie.Expires = expires;
                Save(cookie);
            }
    
    
            /// <summary>
            /// 保存指定名称的单值Cookie
            /// </summary>
            /// <returns></returns>
            public static void SetCookie(string name, string value, string path, DateTime expires)
            {
                HttpCookie cookie = GetCookie(name);
                if (cookie == null)
                {
                    cookie = GetNewCookie(name);
                }
                cookie.Path = path;
                cookie.Value = value;
                cookie.Expires = expires;
                Save(cookie);
            }
    
            /// <summary>
            /// 保存指定名称的单值Cookie
            /// </summary>
            /// <returns></returns>
            public static void SetCookie(string name, string value, string path)
            {
                SetCookie(name, value, path, DateTime.Now);
            }
        }
    }
  • 相关阅读:
    django LDAP
    Python egg
    皮皮书屋
    Linux运维
    bash shell 快捷键汇总
    linux ldconfig
    Linux set env export declare unset
    OpenStack
    【LeetCode】258. Add Digits
    一个"Median Maintenance"问题
  • 原文地址:https://www.cnblogs.com/d685600/p/3666143.html
Copyright © 2020-2023  润新知