创建
//第一种 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())