关于Cookie的设置过期问题
单击注销设置Cookie过期(cookie.Expires = dtNow - tsMin;)后,直接去获取(HttpCookie cookie = Request.Cookies[ "isLogin "];)仍然可以成功,再去获取才会失败。这是什么原因?请高手指点
下面为代码:
protected void LoginOut_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies[ "isLogin "];
if (cookie != null)
{
DateTime dtNow = DateTime.Now;
TimeSpan tsMin = new TimeSpan(0, 0, 10);
cookie.Expires = dtNow - tsMin;
Response.Cookies.Add(cookie);
}
BindData();
}
protected void BindData()
{
HttpCookie cookie = Request.Cookies[ "isLogin "];
if (cookie != null)
{
string strUsr = cookie.Values[ "user "].ToString();
Label2.Text = "欢迎你, " + strUsr;
}
else
{
Label2.Text = "未登录,请登录 ";
}
}
[解决办法]
感觉你写的罗嗦了,直接写
HttpCookie cookie = Request.Cookies[ "isLogin "];
if (cookie != null)
{
cookie.Expires=System.DateTime.Now.AddDays(-1);
}