cookie存取和设置过期时间

创建

//第一种 Response.Cookies["userName"].Value = "patrick"; Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);//存的时候指定过期时间

//第二种 HttpCookie aCookie = new HttpCookie("lastVisit"); aCookie.Value = DateTime.Now.ToString(); aCookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(aCookie);

读取

if(Request.Cookies["userName"] != null) {     HttpCookie aCookie = Request.Cookies["userName"];     Label1.Text = Server.HtmlEncode(aCookie.Value); }

 

设置cookie过期时间

默认cookies失效时间是直到关闭浏览器,cookies失效,也可以指定cookies时间。

Response.Cookies("user_name").Expires=Date+1 '指定cookie保存时间

保留COOKIES一个小时

Response.Cookies("MyCookie").Expires= (now()+1/24)
Response.Cookies("MyCookie").Expires = DateAdd("h", 1, Now())

上一篇:转载 Secure, HttpOnly, SameSite HTTP Cookies Attributes and Set-Cookie Explained


下一篇:3.了解Spring Boot2自动配置原理