views:

560

answers:

3

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?

I tried:

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

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

+2  A: 

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

cgreeno
tried the first suggestion, didn't help..the cookie still has the value.
Mithil Deshmukh
This is the C# syntax the VB will be similar
cgreeno
Ok I added the VB Syntax as pulled directly from the MSDN - Good luck
cgreeno
A: 

This might sound stupid.

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

shahkalpesh
A: 

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.

Marc Novakowski