.net Cookie的操作

來源:互聯網
上載者:User

標籤:==   collect   void   getc   setcookie   ict   div   ado   讀取   

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);        }    }}

 

.net Cookie的操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.