asp.net操作cookie
生活随笔
收集整理的這篇文章主要介紹了
asp.net操作cookie
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、添加cookie?
?
C# 代碼 ??復(fù)制 //方式1: Response.Cookies["username"].value="gggg"; Response.Cookies["username"].Expires=DateTime.MaxValue; //方式2: HttpCookie acookie = new HttpCookie("last"); acookie.Value="a"; acookie..Expires=DateTime.MaxValue; Response.Cookies.Add(acookie); //多值Cookie的寫法 //方式1: Response.Cookies["userinfo1"]["name"].value="aaa"; Response.Cookies["userinfo1"]["last"].value="a"; Response.Cookies["userinfo1"].Expires=DateTime.MaxValue; //方式2: HttpCookie cookie = new HttpCookie("userinfo1"); cookie.Values["name"]="aaa"; cookie.Values["last"]="a"; cookie.Expires=DateTime.MaxValue; //cookie.Expires = System.DateTime.Now.AddDays(1);//設(shè)置過期時(shí)間 1天 Response.Cookies.Add(cookie);?
二、讀取Cookie?
Internet Explorer 將站點(diǎn)的 Cookie 保存在文件名格式為 <user>@<domain>.txt 的文件中,其中 <user> 是您的帳戶名。
?
C# 代碼 ??復(fù)制 if (Request.Cookies["userName"]!=null) { string str = Request.Cookies("userName").Value; } //多值Cookie的讀取 if (Request.Cookies["userInfo1"]!=null ) { string name=Request.Cookies["userInfo1"]["name"]; string last=Request.Cookies["userInfo1"]["last"]; } //讀取 Cookie 集合 for(int i = 0 ;i<Request.Cookies.Count ;i++) { HttpCookie cookies = Request.Cookies; Response.Write("name="+cookies.Mame+"<br/>"); if (cookies.HasKeys )//是否有子鍵 { System.Collections.Specialized.NameValueCollection NameColl = aCookie.Values ; for(int j=0;j<NameColl.Count;j++) { Response.Write("子鍵名="+ NameColl.AllKey[j] +"<br/>"); Response.Write("子鍵值="+ NameColl[j] +"<br/>"); } } else { Response.Write("value="+cookies.Value+"<br/>"); } }
注意:在獲取Cookie的值之前,應(yīng)該確保該 Cookie 確實(shí)存在。否則,您將得到一個(gè)異常
?
三、修改 Cookie?
修改的方法與創(chuàng)建方法相同
?
四、刪除 Cookie?
將其有效期設(shè)置為過去的某個(gè)日期。當(dāng)瀏覽器檢查 Cookie 的有效期時(shí),就會(huì)刪除這個(gè)已過期的 Cookie。
?
?
五、ASP.NET對(duì)Cookie操作的公用類
?
C# 代碼 ??復(fù)制 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Collections.Specialized; namespace Core.Common.Web { /// <summary> /// Cookie靜態(tài)操作類 /// </summary> public static class Cookie { #region 靜態(tài)方法 /// <summary> /// 創(chuàng)建或修改COOKIE對(duì)象并賦Value值 /// </summary> /// <param name="strCookieName">COOKIE對(duì)象名</param> /// <param name="strValue">COOKIE對(duì)象Value值</param> /// <remarks> /// 對(duì)COOKIE修改必須重新設(shè)Expires /// </remarks> public static void SetObject(string strCookieName, string strValue) { SetObject(strCookieName, 1, strValue); } /// <summary> /// 創(chuàng)建或修改COOKIE對(duì)象并賦Value值 /// </summary> /// <param name="strCookieName">COOKIE對(duì)象名</param> /// <param name="iExpires"> /// COOKIE對(duì)象有效時(shí)間(秒數(shù)) /// 1表示永久有效,0和負(fù)數(shù)都表示不設(shè)有效時(shí)間 /// 大于等于2表示具體有效秒數(shù) /// 86400秒 = 1天 = (60*60*24) /// 604800秒 = 1周 = (60*60*24*7) /// 2593000秒 = 1月 = (60*60*24*30) /// 31536000秒 = 1年 = (60*60*24*365) /// </param> /// <param name="strValue">COOKIE對(duì)象Value值</param> /// <remarks> /// 對(duì)COOKIE修改必須重新設(shè)Expires /// </remarks> public static void SetObject(string strCookieName, int iExpires, string strValue) { HttpCookie objCookie = new HttpCookie(strCookieName.Trim()); objCookie.Value = HttpContext.Current.Server.UrlEncode(strValue.Trim()); if (iExpires > 0) { if (iExpires == 1) { objCookie.Expires = DateTime.MaxValue; } else { objCookie.Expires = DateTime.Now.AddSeconds(iExpires); } } HttpContext.Current.Response.Cookies.Add(objCookie); } /// <summary> /// 創(chuàng)建COOKIE對(duì)象并賦多個(gè)KEY鍵值 /// </summary> /// <param name="strCookieName">COOKIE對(duì)象名</param> /// <param name="iExpires"> /// COOKIE對(duì)象有效時(shí)間(秒數(shù)) /// </param> /// <param name="KeyValue">鍵/值對(duì)集合</param> public static void SetObject(string strCookieName, int iExpires, NameValueCollection KeyValue) { HttpCookie objCookie = new總結(jié)
以上是生活随笔為你收集整理的asp.net操作cookie的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sublime Text(2/3)编译l
- 下一篇: 【v2.x OGE教程 18】 Enti