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

查看:168
本文介绍了你如何清除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的

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

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