如何在ASP.NET MVC 4中手动删除Cookie

编程入门 行业动态 更新时间:2024-10-28 05:28:26
本文介绍了如何在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

本文发布于:2023-11-09 22:35:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   ASP   NET   MVC   Cookie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!