你如何清除Cookies使用asp.net MVC 3和C#?
问题描述:
好了,我真的觉得我这样做的权利,但饼干不被清除。
Ok, so I really think I am doing this right, but the cookies aren't being cleared.
Session.Clear();
HttpCookie c = Request.Cookies["MyCookie"];
if (c != null)
{
c = new HttpCookie("MyCookie");
c["AT"] = null;
c.Expires = DateTime.Now.AddDays(-1);
Request.Cookies.Add(c);
}
return RedirectToAction("Index", "Home");
在重定向发生,它再次找到cookie,并就好像我从来没有退出上移动。有什么想法?
When the redirect happens, it finds the cookie again and moves on as though I never logged out. Any thoughts?
答
您正在接近。你需要使用Response对象写回浏览器:
You're close. You'll need to use the Response object to write back to the browser:
if ( Request.Cookies["MyCookie"] != null )
{
var c = new HttpCookie( "MyCookie" );
c.Expires = DateTime.Now.AddDays( -1 );
Response.Cookies.Add( c );
}
在MSDN上的更多信息,请如何删除Cookie的