asp.net中cookie的增,删,改
电脑版发表于:2019/3/4 15:40
添加cookie
protected void Button1_Click(object sender, EventArgs e) { HttpCookie cookie = new HttpCookie("name"); cookie.Value = Server.UrlEncode("小公举"); //设置cookie两个小时后过期 cookie.Expires = DateTime.Now.AddHours(2); //写入cookie Response.Cookies.Add(cookie); }
其实是让response携带一个cookie,让客户端做真正的cookie创建与存储
修改cookie
protected void Button2_Click(object sender, EventArgs e) { HttpCookie cookie = new HttpCookie("name"); cookie.Value = Server.UrlEncode("大公举"); //response携带cookie信息,把这个信息告诉客户端 Response.Cookies.Add(cookie); }
删除cookie
protected void Button3_Click(object sender, EventArgs e) { //取cookie HttpCookie cookie = Request.Cookies["name"]; cookie.Expires = DateTime.Now.AddMinutes(-1);//设置cookie已经过期 Response.Cookies.Add(cookie); }
需要注意理解的是:
因为cookie是浏览器存储的,所以我们后台不能真正的删除cookie,只能是让浏览器帮我们删除一下。
后台的工作就是告诉浏览器帮助删除一下cookie