zoukankan      html  css  js  c++  java
  • 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);
            }
        }
    }
  • 相关阅读:
    [python工具][1]sublime安装与配置
    [办公软件][1]cmder安装
    [持续集成学习篇]【1】[jenkins安装与配置]
    [python篇] [伯乐在线][1]永远别写for循环
    [python学习篇] uiautomator xiaocong
    SharePoint 2010中重置windows 活动目录(AD)域用户密码的WebPart(免费下载)
    使用SharePoint 2010的母版页
    SharePoint 2013 入门教程--系列文章
    自定义 SharePoint 2010 快速启动栏和顶部链接栏
    SharePoint2010 自定义代码登录方法
  • 原文地址:https://www.cnblogs.com/d685600/p/3666143.html
Copyright © 2011-2022 走看看