Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change a cookie value of a cookie that already exists

Tags:

I have a cookie called SurveyCookie. Created like so:

var cookie = new HttpCookie("SurveyCookie"); cookie.Values["surveyPage"] = "1"; cookie.Values["surveyId"] = "1"; cookie.Values["surveyTitle"] = "Definietly not an NSA Survey...."; cookie.Values["lastVisit"] = DateTime.UtcNow.ToString(); cookie.Expires = DateTime.UtcNow.AddDays(30); Response.Cookies.Add(cookie); 

Which works great. Now the problem comes when I want to change the value "surveyPage" like so.

The below will create a new cookie which is not what I want.

int cookieValue = Convert.ToInt32(Request.Cookies["SurveyCookie"]["surveyPage"]) + 1; Response.Cookies["SurveyCookie"]["surveyPage"] = cookieValue.ToString(); 

Then I tried this code below which doesn't work either. The surveyPage is still 1 when it should be 2.

Request.Cookies["SurveyCookie"]["surveyPage"] = cookieValue.ToString();  

Since neither of the above works what does change the cookies value for surveyPage?

like image 406
allencoded Avatar asked Feb 27 '14 14:02

allencoded


People also ask

Can I change the value of a cookie?

You cannot directly modify a cookie. Instead, changing a cookie consists of creating a new cookie with new values and then sending the cookie to the browser to overwrite the old version on the client.

How do you overwrite cookie values?

To update a cookie, simply overwrite its value in the cookie object. You do this by setting a new cookie on the document with the same Name, but a different Value.

Can you overwrite a cookie?

A cookie can only be overwritten (or deleted) by a subsequent cookie exactly matching the name, path and domain of the original cookie. Even though a cookie with domain “.

Which method of response object is used to update the existing cookie?

Instead, you can call the HttpResponse. Cookies. Set method, as the following example shows. Updates an existing cookie in the cookie collection.


1 Answers

From ASP.NET Cookies Overview:

You cannot directly modify a cookie. Instead, changing a cookie consists of creating a new cookie with new values and then sending the cookie to the browser to overwrite the old version on the client.

You can try this:

HttpCookie cookie = Request.Cookies["SurveyCookie"]; if (cookie == null) {     // no cookie found, create it     cookie = new HttpCookie("SurveyCookie");     cookie.Values["surveyPage"] = "1";     cookie.Values["surveyId"] = "1";     cookie.Values["surveyTitle"] = "Definietly not an NSA Survey....";     cookie.Values["lastVisit"] = DateTime.UtcNow.ToString(); } else {     // update the cookie values     int newSurveyPage = int.Parse(cookie.Values["surveyPage"]) + 1;     cookie.Values["surveyPage"] = newSurveyPage.ToString(); }  // update the expiration timestamp cookie.Expires = DateTime.UtcNow.AddDays(30);  // overwrite the cookie Response.Cookies.Add(cookie); 
like image 96
crates_barrels Avatar answered Oct 29 '22 11:10

crates_barrels