Remove or Reset Cookies

一笑奈何 提交于 2019-12-04 12:50:36

You need to use the Response not the Request

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

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

EDIT For 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)

These Examples come right off the MSDN site

SideNote

Often people attempt to use

Request.Cookies.Remove("MyCookie");

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

This might sound stupid.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!