locked
Reading and writing cookies RRS feed

  • Question

  • User1226157895 posted

    In my ASP.net application, I use cookies to hold some user data. So I create a new cookie in Signin button click and add that to Response.Cookies.add followed by response.redirect (to some dashboard page). When this redirect is called, Application_AuthenticateRequest event (in Global.asax.cs ) triggered. Here i will extract the info from cookie (Using Request.Cookies["CookieName"]) and validate the user data.

    Now the issue is Request.Cookies["CookieName"] results old cookie instead of the new one that was written in the Signin.aspx. This issue occurs once in around 5-10 trails and not consistent. Can anyone help me with this. 

    Saturday, March 12, 2016 10:07 AM

Answers

  • User1226157895 posted

    I have wrong JS code in signin button click, which calls signin function twice for single enter key stroke. Problem solved. Thanks for the inputs.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 29, 2016 7:00 AM

All replies

  • User614698185 posted

    Hi Srikanth,

    Welcome to ASP.NET Forums!

    You can direct the user's browser to delete the cookie by setting the cookie's expiration date to a past date. The next time a user makes a request to a page within the domain or path that set the cookie, the browser will determine that the cookie has expired and remove it, like below:

    if (Request.Cookies["UserSettings"] != null)
    {
        HttpCookie myCookie = new HttpCookie("UserSettings");
        myCookie.Expires = DateTime.Now.AddDays(-1);
        Response.Cookies.Add(myCookie);
    }

    Please see: https://msdn.microsoft.com/en-us/library/ms178195.aspx

    Best Regards,

    Candice Zhou

    Monday, March 14, 2016 6:40 AM
  • User1226157895 posted

    I put the same code before adding cookie to response. But issue is still exists. As I told you issue comes randomly. I get old cookie if cookie already present else null. 

    Monday, March 14, 2016 4:32 PM
  • User614698185 posted

    Hi Srikanth,

    I think you could set original Cookies to be null before executing, then assign the Cookie as you want.

    Best Regards,

    Candice Zhou

    Wednesday, March 16, 2016 10:14 AM
  • User1226157895 posted

    I have wrong JS code in signin button click, which calls signin function twice for single enter key stroke. Problem solved. Thanks for the inputs.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 29, 2016 7:00 AM