单击注销设置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 = "未登录,请登录";
}
}
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);
}
HttpCookie cookie = Request.Cookies["isLogin"];
if (cookie != null)
{
cookie.Expires=System.DateTime.Now.AddDays(-1);
}
Response.Cookies.Add(cookie);
关键是最后还有把这个修改的cookie加回去;否则没有效果
if (cookie != null)
{
cookie.Expires=System.DateTime.Now.AddDays(-1);}
Response.Cookies.Add(cookie);
最后要把这个修改过的COOKIE加回去,否则没用的,
不好意思,希望没有影响到你