asp.net-mvc – Cookie不会被删除
发布时间:2020-12-15 19:04:16 所属栏目:asp.Net 来源:网络整理
导读:我使用以下代码在我的asp.net mvc(C#)应用程序中设置一个cookie: public static void SetValue(string key,string value,DateTime expires){ var httpContext = new HttpContextWrapper(HttpContext.Current); _request = httpContext.Request; _response =
我使用以下代码在我的asp.net mvc(C#)应用程序中设置一个cookie:
public static void SetValue(string key,string value,DateTime expires) { var httpContext = new HttpContextWrapper(HttpContext.Current); _request = httpContext.Request; _response = httpContext.Response; HttpCookie cookie = new HttpCookie(key,value) { Expires = expires }; _response.Cookies.Set(cookie); } 我需要在用户点击注销时删除Cookie。设置的Cookie不是使用清除/删除删除/删除。代码如下: public static void Clear() { var httpContext = new HttpContextWrapper(HttpContext.Current); _request = httpContext.Request; _response = httpContext.Response; _request.Cookies.Clear(); _response.Cookies.Clear(); } public static void Remove(string key) { var httpContext = new HttpContextWrapper(HttpContext.Current); _request = httpContext.Request; _response = httpContext.Response; if (_request.Cookies[key] != null) { _request.Cookies.Remove(key); } if (_response.Cookies[key] != null) { _response.Cookies.Remove(key); } } 我已经尝试了两个以上的功能,但仍然cookie存在,当我尝试检查存在。 public static bool Exists(string key) { var httpContext = new HttpContextWrapper(HttpContext.Current); _request = httpContext.Request; _response = httpContext.Response; return _request.Cookies[key] != null; } 这里可能有什么问题?或什么,我需要做的事情删除/删除cookie? 解决方法
清除响应的Cookie不会指示浏览器清除Cookie,它只是不会将Cookie发送回浏览器。要指示浏览器清除Cookie,您需要告诉它Cookie已过期,例如
public static void Clear(string key) { var httpContext = new HttpContextWrapper(HttpContext.Current); _response = httpContext.Response; HttpCookie cookie = new HttpCookie(key) { Expires = DateTime.Now.AddDays(-1) // or any other time in the past }; _response.Cookies.Set(cookie); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 如何使用MVC 4上传大文件?
- 安装程序 – 在MVC4 RC中缺少单页应用程序模板
- 在ASP.NET MVC 2中实现DropDownList的最佳方式?
- 我可以在Razor中使用@helper语法返回一个字符串吗?
- asp.net-core – db ASP.NET Core中的临时保存密码
- asp.net-mvc – ASP.NET MVC RC中的Html.Image在哪里?
- asp.net-mvc-3 – MVC3 MapRoute,带斜杠的参数
- jqgrid与asp.net webmethod和json使用排序,分页,搜索和LINQ
- asp.net – 如何减缓黑客的速度
- asp.net-mvc-3 – mvc3 – 如何从源代码禁用符号的htmlenco
推荐文章
站长推荐
- asp.net – Crystal Report仅显示第一页
- asp.net – MVC5发布到Azure,因为云服务在AD登录
- asp.net-mvc – 如何在ASP.NET MVC中为自定义Use
- asp.net-mvc-3 – MVC:为此对象定义的无参数构造
- ASP MVC-3:在发布帖子后更新AJAX表单数据时出现
- Asp.net MVC json还是Json.net?
- ASP.NET 预编译命令(解决发布后第一次访问慢问题
- asp.net-mvc – ASP.NET MVC FileStreamResult不
- ASP.NET潜在危险请求Javascript正则表达式
- asp.net – 升级到.net 4.0后无法加载文件或程序
热点阅读