DDiVita
DDiVita

Reputation: 4265

Cookies and ASP.NET driving me crazy

I have created a simple shopping cart application. We needed something specific to our needs, long story. Anyway, I am storing the cart object in a cookie. That work fine, but I am having trouble with deleting the cart cookie from within the class. The cart object contains a collection of products (iList). Here is the code I use to delete the cookie: My Empty Cart Code:

Dim currentCookie As HttpCookie = HttpContext.Current.Response.Cookies(cookieName)
currentCookie.Expires = DateTime.Now.AddYears(-30)
HttpContext.Current.Response.Cookies.Add(currentCookie)

My LoadCartFromCookie code:

if not HttpContext.Current.Request.Cookies(theCookieName) is nothing then
     _cart =  CType(HttpContext.Current.Request.Cookies(theCookieName).value,Cart)
End If

My cart class constructor first tries to load the cart from the cookie. If it finds the cookie then it loads the cart object otherwise it creates a new instance of the cart without any details. For some reason even if I run the delete cookie (Empty cart) code and then run my LoadCartFromCookie code (From inside the Cart class) it still loades the expired cookie. Any thoughts? I thought it might have been a browser issue, but I tried IE8, FF 3.5, and Chrome. If inside the codebehind for and ASPX page I try looking for the expired cookie (Request.Cookies(theName)) , it never finds it. Which is what I want it to do inside the class.

Daniel

Upvotes: 3

Views: 723

Answers (3)

Sky Sanders
Sky Sanders

Reputation: 37084

are you running the statements back to back or on the next postback?

You are just loading an expired cookie because you didn't remove a cookie, you just added an expired cookie, no?

Enter what seems like a catch 22 but is actually a code smell....

To delete a cookie on the browser you have to send an expired cookie. But if you are using a cookie as a data object in your code on the same cycle you then have to manage the cookie collection.

Either dedicate one postback to just deleting the cookie or use a clr object to keep track of your cart while in the codebehind. e.g. load the cookie into an object on page_load and set the cookie from the object before you exit but do not reference the Cookies collection.

p.s. While I haven't wrasseled with cookies for a while, if you want to REPLACE a cookie, try Cookies.Remove() and add the new one, it should replace the old one on the browser while keeping a single valid cookie in the collection for you. But I still don't recommend using the cookie collection as data storage in your code behind classes...

Upvotes: 2

zihotki
zihotki

Reputation: 5191

For some reason even if I run the delete cookie (Empty cart) code and then run my LoadCartFromCookie code (From inside the Cart class) it still loades the expired cookie.

It seems to me that you are doing this in the same request. Cookie is deleted in client browser only after a response from server with updated cookie with some expired date. So update your code in "LoadCartFromCookie" to check for expiration date too.
And IMO it's much more better to keep a cart information in the database. So it'll be shared between all browsers.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039110

To expire the cookie try:

Dim cookie = HttpContext.Current.Request.Cookies(cookieName)
If Not cookie Is Nothing Then
    cookie.Expires = DateTime.Now.AddYears(-1)
    HttpContext.Current.Response.SetCookie(currentCookie)
End If

Upvotes: 0

Related Questions