如何在ASP.NET MVC 4中手动删除Cookie [英] How do I manually delete a cookie in asp.net MVC 4

查看:177
本文介绍了如何在ASP.NET MVC 4中手动删除Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要手动删除身份验证cookie(而不是出于某些原因而不使用FormsAuthentication.SignOut无效).我尝试过

I need to delete authentication cookie manually (Instead of using FormsAuthentication.SignOut whcih for some reasons does not work). I tried

System.Web.HttpContext.Request.Cookies.Remove(cookieName); // for example .ASPXAUTH
System.Web.HttpContext.Response.Cookies.Remove(cookieName); // for example .ASPXAUTH
FormsAuthentication.SignOut(); // I don't know why this one does not work

这两个命令都不起作用.实际上,响应cookie是空的,请求cookie包含我要在执行以下命令时删除的cookie,它不再包含我删除的cookie,但在浏览器中该cookie仍然存在,即使授权后,我也可以执行授权用户可以执行的操作登出.

Neither of those command work. In fact Response cookies are empty and request cookie contains the cookie I want to delete when the following commands are executed it no longer contains the cookie I deleted but in browser the cookie still exists and I am able to do things that authorized users can even after signing out.

推荐答案

尝试:

if ( Request.Cookies["MyCookie"] != null )
{
    var c = new HttpCookie( "MyCookie" );
    c.Expires = DateTime.Now.AddDays( -1 );
    Response.Cookies.Add( c );
}

有关 MSDN 的详细信息.

这篇关于如何在ASP.NET MVC 4中手动删除Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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