vs2005的cookies删除
前言:一个朋友在一个项目中提到,到底如何删除cookies.网络里也有很多人提出这样的问题。
现把我的解决方式共享一下
代码很简单,自己看看就明白
public partial class Cookies : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//清除cookies
// Request.Cookies..clear();
//将cookies["username"]置空
HttpCookie usercookie = Request.Cookies["username"];
usercookie.Expires = System.DateTime.Now.AddDays(-10);
// usercookie.Expires=.AddDays(-1.0);
Response.Cookies.Add(usercookie);
}
protected void Button2_Click(object sender, EventArgs e)
{
//读取cookies
HttpCookie mycookie = Request.Cookies["username"];
Response.Write(mycookie.Value);
}
protected void Button3_Click(object sender, EventArgs e)
{
//写入cookies
HttpCookie usercookie = new HttpCookie("username", "test");
//usercookie.Expires = System.DateTime.Now.AddMinutes(1);
usercookie.Expires = System.DateTime.Now.AddDays(1);
Response.Cookies.Add(usercookie);
}
}
综上所述,其实方法就是将时间改为失效时间点