如何使用 asp.net mvc 3 和 c# 清除 cookie? [英] How do you clear cookies using asp.net mvc 3 and c#?

查看:23
本文介绍了如何使用 asp.net mvc 3 和 c# 清除 cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我真的认为我这样做是正确的,但是 cookie 没有被清除.

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.

More information on MSDN, How to: Delete a Cookie.

这篇关于如何使用 asp.net mvc 3 和 c# 清除 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆