删除或重置Cookie [英] Remove or Reset Cookies

查看:289
本文介绍了删除或重置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序的页面之一(第3页)上设置Cookie Request.Cookies( TemplateName)。value 。现在,我可以从第3页导航到第4页和第2页,并保留cookie的值。但是现在当我注销并再次登录时,它仍然具有该值,在启动新实例时如何将cookie的值重置为空白?

I am setting a cookie Request.Cookies("TemplateName").value on one of my pages(page 3) of my application. Now I can navigate from page 3 to page 4 and page 2 and retain the value of the cookie. But now when I logout and login again it still has the value, how can I reset the value of the cookie to be blank "" when I start a new instance?

我尝试过:

Request.Cookies("TemplateName").Expires = Now
Request.Cookies("TemplateName").value = "" 

在我的首页上,但Cookie仍保留第2页和3。

On my homepage, but the cookie still retains the value on page 2 and 3.

推荐答案

您需要使用响应而不是请求

You need to use the Response not the Request

Response.Cookies["TemplateName"].Value = "";

Response.Cookies["TemplateName"].Expires = DateTime.Now;

为VB编辑。

Dim subkeyName As String
subkeyName = "userName"
Dim aCookie As HttpCookie = Request.Cookies("userInfo")
aCookie.Values.Remove(subkeyName)
aCookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(aCookie)


Response.Cookies("userName").Value = "patrick"
Response.Cookies("userName").Expires = DateTime.Now.AddDays(1)

这些示例直接来自 MSDN网站

SideNote

经常有人尝试使用

Request.Cookies.Remove("MyCookie");

仅从请求集合中删除Cookie,如果要删除Cookie,则您需要使其过期。更多信息此处

Which will only remove the cookie from the "request collection", If you want to remove a cookie then you need to expire it. More info here

这篇关于删除或重置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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