5

I have spent sooooo many hours on this it is insane.

I have a page base class which contains a "setcookie" function and this is basically this:

        Dim context As HttpContext = System.Web.HttpContext.Current

        If context.Request.Cookies(cookieName) Is Nothing Then
            Dim cookie As HttpCookie
            cookie.Value = cookieValue
            cookie.Expires = DateTime.Now.AddDays(7)
            context.Response.Cookies.Add(cookie)
        Else
            Dim cookie As HttpCookie = context.Request.Cookies(cookieName)
            cookie.Expires = DateTime.Now.AddDays(7)
            cookie.Value = cookieValue
        End If

This function is called by a simple aspx page. As this is in a test environment there is a previous value of "123" in the cookie I'm using. If I use the debug and watch window, I see the value change to "168" successfully.

I have a debug break point on a line which is:

           Response.Redirect("overview.aspx", False)

When the break point is active, the values in the watch window are:

    currProjectID   168 Integer
    HttpContext.Current.Request.Cookies("currProjectID").Value  "168"   String

(currProjectID is a property in the basepage class that gets/sets the cookie with the function above)

Now, the second I step off the breakpoint line above using "F10" the values of the variable change!

    HttpContext.Current.Request.Cookies("currProjectID").Value  "123"   String
    currProjectID   123 Integer

This is insane! The code goes nowhere, the debug point is immediately under the "response.redirect" line above and yet the values have immediately changed to what they were before! Nothing has gone near the "setcookie" routine so please please please would someone save my insanity and tell me what is going wrong!?

TheMook
  • 1,531
  • 4
  • 20
  • 58
  • I have carried on playing with it. Still no joy :-( The immediate watch window shows the correct updated value until I step over the "response.redirect" line, at which point it instantly changes back to its initial value and without going through the "setcookie" function. I really don't see how this value can be changing in this way. – TheMook Mar 14 '12 at 23:21
  • Gave up. Reverted to session based variables in SQL server. – TheMook Mar 15 '12 at 14:14

1 Answers1

3

You have to: - get the cookie from request - update cookie - send cookie in response

If you dont sent cookie in the response, browser wouldn't know anything about the change !!! Cookies aren't that clever to update themselves.

Hope it helps.

UPDATE

var cookieDetails = Request.Cookies["yourCookie"];
if (cookieDetails != null)
{
    cookieDetails.Values["someValue"] = valueToAssign;
}
else
{
    cookieDetails = new HttpCookie("yourCookie");
    cookieDetails.Values.Add("someValue", valueToAssign);
}
Response.Cookies.Add(cookieDetails);

this example sets a cookie. as you can see the first bit is checking if cookie exists and the second just creates new cookie.

you are missing the last bit which sends the cookie back to browser

Response.Cookies.Add(cookieDetails);

Hope it helps.

Sebastian Siek
  • 2,045
  • 17
  • 16
  • Yes, We can exactly get the cookie value in this way as you mentioned and hopefully OP is doing the same. – Pankaj Mar 14 '12 at 19:08
  • I don't understand, sorry... This bit of code from my function does exactly that, doesn't it? Dim cookie As HttpCookie = context.Request.Cookies(cookieName) cookie.Expires = DateTime.Now.AddDays(7) cookie.Value = cookieValue – TheMook Mar 14 '12 at 21:03
  • sorry, I should have been more specific in my answer. will update it in a second and will include example. – Sebastian Siek Mar 14 '12 at 21:20
  • Thanks for the update... but that doesn't seem to be the problem - I used the example from the MSDN site and where the cookie already exists, all you need to do is update the value and optionally the expiry date. I tried changing it to your version and it seems to add a duplicate set of cookies when I use fiddler to inspect! – TheMook Mar 14 '12 at 22:17
  • This works great for me. My problem was that I didn't use "Request.Cookies" to update the values. Using this fixed it thanks a bunch!! – Kaos Oct 31 '12 at 18:18