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.
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);
}
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.
None
0 Points
3 Posts
Reading and writing cookies
Mar 12, 2016 10:07 AM|Srikanth Varma|LINK
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.
Star
12330 Points
2021 Posts
Re: Reading and writing cookies
Mar 14, 2016 06:40 AM|Candice Zhou|LINK
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:
Please see: https://msdn.microsoft.com/en-us/library/ms178195.aspx
Best Regards,
Candice Zhou
None
0 Points
3 Posts
Re: Reading and writing cookies
Mar 14, 2016 04:32 PM|Srikanth Varma|LINK
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.
Star
12330 Points
2021 Posts
Re: Reading and writing cookies
Mar 16, 2016 10:14 AM|Candice Zhou|LINK
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
None
0 Points
3 Posts
Re: Reading and writing cookies
Mar 29, 2016 07:00 AM|Srikanth Varma|LINK
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.