.net Cookie的操作

2018-06-22 07:49:37来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

namespace Zhong.Core
{
    /// <summary>
    /// Cookie操作类
    /// </summary>
    public class CookieHelper
    {
        private static readonly string CookieName = "Zhong";
        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="values">键/值对</param>
        /// <param name="expires">过期超时时间(秒),为0时不设置过期时间</param>
        /// <param name="domain">域名</param>
        /// <param name="path">路径</param>
        public static void SetCookie(string name, Dictionary<string, 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 (KeyValuePair<string, 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);
        }
        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetCookie(string key, string value)
        {
            SetCookie(CookieName, new Dictionary<string, string> { { key, value } }, 0);
        }
        /// <summary>
        /// 根据名称与键读取Cookie
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="key"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// 根据键读取Cookie
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetCookie(string key)
        {
            return GetCookie(CookieName, key);
        }
        /// <summary>
        /// 根据名称获取Cookie
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public static string GetCookieByName(string name)
        {
            string returnVal = null;
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            if (cookie!= null)
            {
                returnVal = cookie.Value;
            }
            return returnVal;
        }
        /// <summary>
        /// 删除Cookie
        /// </summary>
        /// <param name="name">名称</param>
        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);
        }
    }
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:IIS与asp.net管道

下一篇:asp.net 一般处理程序接收上传文件的问题