.net Cookie的操作

2021-06-20 00:03

阅读:519

标签:==   collect   void   getc   setcookie   ict   div   ado   读取   

using System;
using System.Collections.Generic;
using System.Web;

namespace Zhong.Core
{
    /// 
    /// Cookie操作类
    /// 
    public class CookieHelper
    {
        private static readonly string CookieName = "Zhong";
        /// 
        /// 设置Cookie
        /// 
        /// 名称
        /// 键/值对
        /// 过期超时时间(秒),为0时不设置过期时间
        /// 域名
        /// 路径
        public static void SetCookie(string name, Dictionarystring, string> values, int expires, string domain = null, string path = null)
        {
            HttpCookie cookie = HttpContext.Current.Response.Cookies[name];
            if (cookie == null)
            {
                cookie = new HttpCookie(name);
            }
            foreach (KeyValuePairstring, string> kv in values)
            {
                cookie.Values.Add(kv.Key, kv.Value);
            }
            if (domain != null)
            {
                cookie.Domain = domain;
            }
            if (path != null)
            {
                cookie.Path = path;
            }
            if (expires != 0)
            {
                cookie.Expires = DateTime.Now.AddSeconds(expires);  //过期时间
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// 
        /// 设置Cookie
        /// 
        /// 
        /// 
        public static void SetCookie(string key, string value)
        {
            SetCookie(CookieName, new Dictionarystring, string> { { key, value } }, 0);
        }
        /// 
        /// 根据名称与键读取Cookie
        /// 
        /// 名称
        /// 
        /// 
        public static string GetCookie(string name, string key)
        {
            string returnVal = null;
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            if (cookie != null)
            {
                returnVal = cookie[key];
            }
            return returnVal;
        }
        /// 
        /// 根据键读取Cookie
        /// 
        /// 
        /// 
        public static string GetCookie(string key)
        {
            return GetCookie(CookieName, key);
        }
        /// 
        /// 根据名称获取Cookie
        /// 
        /// 名称
        /// 
        public static string GetCookieByName(string name)
        {
            string returnVal = null;
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            if (cookie!= null)
            {
                returnVal = cookie.Value;
            }
            return returnVal;
        }
        /// 
        /// 删除Cookie
        /// 
        /// 名称
        public static void DeleteCookie(string name)
        {
            HttpCookie cookie = HttpContext.Current.Response.Cookies[name];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddYears(-1);
                cookie.Values.Clear();
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
    }
}

 

.net Cookie的操作

标签:==   collect   void   getc   setcookie   ict   div   ado   读取   

原文地址:http://www.cnblogs.com/godbell/p/7190987.html


评论


亲,登录后才可以留言!