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 MVC下使用AngularJs語言(七):Cookie的使用ASP.NETMVCAngularJSCookie
- ASP.NET之CookieASP.NETCookie
- Flask - Cookie的使用FlaskCookie
- jQuery的Cookie使用jQueryCookie
- 如何使用 C# 中的 ValueTaskC#
- 如何使用 C# 中的 LazyC#
- cookie與session的使用CookieSession
- Gin(八):cookie的使用Cookie
- 介面測試--apipost中cookie管理器的使用APICookie
- ASP.NET Core Authentication系列(三)Cookie選項ASP.NETCookie
- 理解ASP.NET Core - 基於Cookie的身份認證(Authentication)ASP.NETCookie
- C#中普通快取的使用C#快取
- js-cookie.js的使用JSCookie
- C# ASP.NET Core 中 IWebHostEnvironment 介面的作用是什麼?C#ASP.NETWeb
- jwt-在asp.net core中的使用jwtJWTASP.NET
- 如何獲取Cookie並使用Cookie侵入Cookie
- Tomcat 中的 Session 和 CookieTomcatSessionCookie
- 140 form自定錯誤和鉤子,django中cookie的使用ORMDjangoCookie
- C# 爬蟲—-Cookies處理(Set-Cookie)C#爬蟲Cookie
- C#中的char和string的使用簡介C#
- Laravel 之 Cookie 使用LaravelCookie
- 【swagger】C# 中 swagger 的使用及避坑SwaggerC#
- ASP.NET與.NET Framework和C#的關係ASP.NETFrameworkC#
- requests庫中的Cookie處理Cookie
- flask框架中的cookie和sessionFlask框架CookieSession
- ASP.NET Core Web Api之JWT VS Session VS Cookie(二)ASP.NETWebAPIJWTSessionCookie
- C#/Vsto中CustomTaskPanes和Ribbon的使用方法C#
- ASP.NET Razor – C# 變數簡介ASP.NETC#變數
- [C#]C#中字串的操作C#字串
- ASP.NET Core之身份驗證和授權Cookie&SessionASP.NETCookieSession
- 淺談ASP.NET Core中IOC與DI的理解和使用ASP.NET
- C# ExpandoObject的使用C#Object
- C# RabbitMQ的使用C#MQ
- localStorage,sessionStorage和cookie的區別及使用SessionCookie
- cookie的使用方法以及注意事項Cookie
- npm包:js-cookie的簡單使用NPMJSCookie
- Widows中伺服器中cookie檔案的位置伺服器Cookie
- C#中的MVCC#MVC
- C#中的MVVMC#MVVM