asp.net-mvc – Cookie不会被删除
发布时间:2020-05-23 07:58:18 所属栏目:asp.Net 来源:互联网
导读:我使用以下代码在我的asp.net mvc(C#)应用程序中设置一个cookie: public static void SetValue(string key, string value, DateTime expires){ var httpContext = new HttpContextWrapper(HttpContext.Current); _request = ht
|
我使用以下代码在我的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-3 – 从IIS7中的虚拟目录运行ASP.NET MVC应用程
- asp.net – MVC 2 AntiForgeryToken – 为什么对称加密IPri
- asp.net – 使用Elmah处理Web服务中的异常
- ASP.NET线程敏捷 – 如何克服?
- asp.net-mvc – 应用程序服务层作为静态类
- 全局错误登录ASP.Net MVC 6
- asp.net-mvc-3 – Asp.Net MVC 3自定义WebViewPage在不同的
- asp.net-mvc – 将焦点放在不使用JavaScript的EditorFor上
- asp.net-mvc – ViewBag vs Model,在MVC.NET中
- asp.net-mvc – 使用WCF/OData作为访问层而不是直接使用EF/
推荐文章
站长推荐
- asp.net-mvc – 如何将模型传递给部分视图
- asp.net-mvc – 如何使用UpdateModel进行单元测试
- 如何利用ASP.net IIS 7.5中的浏览器缓存
- asp.net-mvc – 首次使用ASP.NET MVC时的主要风险
- asp.net – Session Timeout .NET
- ASP.NET成员资格框架有多安全?
- asp.net-mvc – Ajax Request返回HTTP错误500,使
- asp.net – 如何在EF Core中向Identity用户添加外
- asp.net – 缓存特定的Javascript和CSS文件
- asp.net – 检查ValidationGroup是否从代码隐藏有
热点阅读
