日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

asp.net操作cookie

發布時間:2025/7/14 asp.net 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 asp.net操作cookie 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、添加cookie?

?

C# 代碼 ??復制 //方式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);//設置過期時間 1天 Response.Cookies.Add(cookie);

?

二、讀取Cookie?

Internet Explorer 將站點的 Cookie 保存在文件名格式為 <user>@<domain>.txt 的文件中,其中 <user> 是您的帳戶名。

?

C# 代碼 ??復制 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的值之前,應該確保該 Cookie 確實存在。否則,您將得到一個異常

?

三、修改 Cookie?


修改的方法與創建方法相同

?

四、刪除 Cookie?


將其有效期設置為過去的某個日期。當瀏覽器檢查 Cookie 的有效期時,就會刪除這個已過期的 Cookie。
?

C# 代碼 ??復制 //刪除cookie下的屬性HttpCookie acookie=Request.Cookies["Info"]; acookie.Values.Remove("userid"); acookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(acookie); //刪除所有cookie,就是設置過期時間為現在就行了 int limit=Request.Cookies.Count - 1; for(int i=0;i<limit;i++) { acookie = Request.Cookies(i) acookie.Expires = DateTime.Now.AddDays(-1) Response.Cookies.Add(acookie) }

?

五、ASP.NET對Cookie操作的公用類

?

C# 代碼 ??復制 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靜態操作類 /// </summary> public static class Cookie { #region 靜態方法 /// <summary> /// 創建或修改COOKIE對象并賦Value值 /// </summary> /// <param name="strCookieName">COOKIE對象名</param> /// <param name="strValue">COOKIE對象Value值</param> /// <remarks> /// 對COOKIE修改必須重新設Expires /// </remarks> public static void SetObject(string strCookieName, string strValue) { SetObject(strCookieName, 1, strValue); } /// <summary> /// 創建或修改COOKIE對象并賦Value值 /// </summary> /// <param name="strCookieName">COOKIE對象名</param> /// <param name="iExpires"> /// COOKIE對象有效時間(秒數) /// 1表示永久有效,0和負數都表示不設有效時間 /// 大于等于2表示具體有效秒數 /// 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對象Value值</param> /// <remarks> /// 對COOKIE修改必須重新設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> /// 創建COOKIE對象并賦多個KEY鍵值 /// </summary> /// <param name="strCookieName">COOKIE對象名</param> /// <param name="iExpires"> /// COOKIE對象有效時間(秒數) /// </param> /// <param name="KeyValue">鍵/值對集合</param> public static void SetObject(string strCookieName, int iExpires, NameValueCollection KeyValue) { HttpCookie objCookie = new

總結

以上是生活随笔為你收集整理的asp.net操作cookie的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。