5

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

我试过:

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

在我的主页上,但 cookie 仍然保留第 2 页和第 3 页的值。

4

3 回答 3

6

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

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 站点

边注

人们经常尝试使用

Request.Cookies.Remove("MyCookie");

这只会从“请求集合”中删除 cookie,如果要删除 cookie,则需要使其过期。更多信息在这里

于 2008-12-22T22:11:26.280 回答
0

这听起来可能很愚蠢。

But are you trying to set cookie from any other place? Search for the code for TemplateName, if that helps.

于 2008-12-22T22:30:32.403 回答
0

I'm not as familiar with .Net but with web apps in general you need to make sure that you set your response headers before writing out any body, otherwise they may not be sent. Just something to double check.

于 2008-12-22T22:39:46.300 回答