30

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?

allencoded
  • 7,015
  • 17
  • 72
  • 126

3 Answers3

60

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);
crates_barrels
  • 988
  • 13
  • 15
  • Cookie received in request can have different properties than cookie previously send to browser. For example when sending cookie with SameSite=Lax then we receive it with SameSite=None. When we change the value and add the same cookie to response then we send to browser also SameSite property changes. – CoperNick Jul 07 '20 at 18:12
  • Nowhere in my C# Razor page can I find the HttpCookie cookie = Request.Cookies["MyCookieName"]; . . . . .it just underlines the "HttpCookie" with a red line and no amount of Using statements gives me that class or whatever, – JustJohn Jun 19 '23 at 23:59
10

You should always create a new cookie each time you need to modify an existing one , the following works for me :

var cookie = new System.Web.HttpCookie("SurveyCookie");
cookie.Values["surveyPage"] =  newValue;
cookie.Expires = DateTime.Now.AddDays(1000);
cookie.SameSite = System.Web.SameSiteMode.None;
cookie.Secure = true;
this.HttpContext.Response.Cookies.Add(cookie);
-1

Check out the Response.SetCookie() method as this will set update your existing cookie

heymega
  • 9,215
  • 8
  • 42
  • 61
  • 8
    _HttpResponse.SetCookie()_ supports the product infrastructure and is not intended to be used directly from your code. The correct way to update a cookie according to Microsoft's _HttpResponse.SetCookie()_ documentation is _Response.Cookies.Add(MyCookie);_. Refer to the example at the bottom of https://msdn.microsoft.com/en-us/library/system.web.httpresponse.setcookie(v=vs.110).aspx – Aaron Newton Jun 24 '16 at 03:33