C# httpcookie asp.net中cookie的使用
今天有空就把操作cookie的寫了,雖然很簡單,不過免得到時候忘記了,之前就是忘記了還很實驗了一番才弄出來,鬱悶了。
下面是寫cookie
1 HttpCookie cookie = new HttpCookie("Info");//定義cookie物件以及名為Info的項
2 DateTime dt = DateTime.Now;//定義時間物件
3 TimeSpan ts=new TimeSpan(1,0,0,0);//cookie有效作用時間,具體查msdn
4 cookie.Expires = dt.Add(ts);//新增作用時間
5 cookie.Values.Add("user","cxbkkk");//增加屬性
6 cookie.Values.Add("userid","1203");
7 Response.AppendCookie(cookie);//確定寫入cookie中 讀取cookie
1 if(Request.Cookies["Info"]!=null)
2 {
3 string temp=Convert.ToString(Request.Cookies["Info"].Values["user"])+" "+Convert.ToString(Request.Cookies["Info"].Values["userid"]);
4 //讀全部就用Request.Cookies["Info"].Value)
5 if(temp=="")
6 {
7 Response.Write("空");
8 }
9 else
10 Response.Write(temp);
11 }
12 else
13 {
14 Response.Write("error");
15 } 修改cookie
1 Response.Cookies["Info"]["user"] = "2";
2 Response.Cookies["Info"].Expires = DateTime.Now.AddDays(1); 刪除cookie下的屬性
1 HttpCookie acookie=Request.Cookies["Info"];
2 acookie.Values.Remove("userid");
3 acookie.Expires = DateTime.Now.AddDays(1);
4 Response.Cookies.Add(acookie); 刪除所有cookie,就是設定過期時間為現在就行了
1 int limit=Request.Cookies.Count - 1;
2 for(int i=0;i
4 acookie = Request.Cookies(i)
5 acookie.Expires = DateTime.Now.AddDays(-1)
6 Response.Cookies.Add(acookie)
7 }
"C#" %> SPAN style="COLOR: maroon">"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">"server"> protected void Page_Load(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); // Get cookie from the current request. HttpCookie cookie = Request.Cookies.Get("DateCookieExample"); // Check if cookie exists in the current request. if (cookie == null) { sb.Append("Cookie was not received from the client. "); sb.Append("Creating cookie to add to the response.
"); // Create cookie. cookie = new HttpCookie("DateCookieExample"); // Set value of cookie to current date time. cookie.Value = DateTime.Now.ToString(); // Set cookie to expire in 10 minutes. cookie.Expires = DateTime.Now.AddMinutes(10d); // Insert the cookie in the current HttpResponse. Response.Cookies.Add(cookie); } else { sb.Append("Cookie retrieved from client.
"); sb.Append("Cookie Name: " + cookie.Name + "
"); sb.Append("Cookie Value: " + cookie.Value + "
"); sb.Append("Cookie Expiration Date: " + cookie.Expires.ToString() + "
"); } Label1.Text = sb.ToString(); } "server">HttpCookie Example "form1" runat="server">
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-564907/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 關於ASP.NET/C#中對Cookie的操作ASP.NETC#Cookie
- 【仰天一笑】asp.net c#中對cookie的操作ASP.NETC#Cookie
- Asp.Net MVC 中的 Cookie(譯)ASP.NETMVCCookie
- asp.net cookieASP.NETCookie
- Asp.net中Cookie簡明參考 (轉)ASP.NETCookie
- ASP.NET MVC下使用AngularJs語言(七):Cookie的使用ASP.NETMVCAngularJSCookie
- ASP.NET之CookieASP.NETCookie
- ASP.Net 加密 CookieASP.NET加密Cookie
- js中cookie的使用詳細分析JSCookie
- ASP.NET中Cookie程式設計簡明參考ASP.NETCookie程式設計
- ASP.NET MVC使用Filter解除Session, Cookie等依賴ASP.NETMVCFilterSessionCookie
- 不使用 Cookie 的“Cookie”技術Cookie
- C#中WebBrowser的使用C#Web
- jQuery中cookie外掛如何使用jQueryCookie
- C# 和 JavaScript Cookie 共享C#JavaScriptCookie
- Flask - Cookie的使用FlaskCookie
- jQuery的Cookie使用jQueryCookie
- PHP cookie的使用PHPCookie
- 如何使用 C# 中的 LazyC#
- 如何使用 C# 中的 ValueTaskC#
- C#中using的使用C#
- 學習心得:asp.net操作cookieASP.NETCookie
- 介面測試--apipost中cookie管理器的使用APICookie
- ASP.NET中使用Cookie儲存使用者名稱和密碼 (轉)ASP.NETCookie密碼
- asp.net網頁中呼叫c#寫的dll檔案ASP.NET網頁C#
- asp中的cookie (轉)Cookie
- Python中Cookie的處理(一)Cookie庫PythonCookie
- 聊聊asp.net中Web Api的使用ASP.NETWebAPI
- ASP.NET 2.0中XSLT的使用ASP.NET
- Gin(八):cookie的使用Cookie
- Cookie的簡單使用Cookie
- cookie與session的使用CookieSession
- Cookie的建立使用 (轉)Cookie
- C#中普通快取的使用C#快取
- C#中類的使用舉例C#
- asp.net教程-C#中使用XML——編寫XMLASP.NETC#XML
- Asp.Net中Response.Cookies.Remove 無法刪除COOKIE的問題解決方法ASP.NETCookieREM
- C#中列舉型別的使用C#型別