Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cant delete a cookie immediatly during execution PostBack - VB.NET

Posted on 2006-11-28
8
Medium Priority
?
615 Views
Last Modified: 2008-02-01
Hi,

I need to delete/expire a cookie in realtime while processing code. This is for when I empty my shopping cart, I want the cookie gone before the cart (which is ascx web control) processes as part of the normal postback procedure.

Can anyone tell me why this code doesn't immediatly expire the cookie?

        ' Set a cookie
        System.Web.HttpContext.Current.Response.Cookies("GUID").Value = "HelloWorld"

        ' expire cookie
        Dim aCookie As HttpCookie = Request.Cookies("GUID")
        aCookie.Expires = DateTime.Now.AddDays(-1)
        Response.Cookies.Add(aCookie)

        ' see if we can still read the cookie, WE CAN!!! Not what we want!  
         Label1.Text = System.Web.HttpContext.Current.Request.Cookies("GUID").Value

After the postback event has completed the cookie is expired, but I really need the cookie expired before I finish executing my postback code!

I know I can do something like Response.Redirect(Request.Url.ToString) at the end of all my code to force 'another' refresh of my page to a pre postback state, but this means about 10 trips back to the database etc.
0
Comment
Question by:bhermer
8 Comments
 
LVL 5

Expert Comment

by:prajapati84
ID: 18034686
Just try this one, u will find what u want...

Label1.Text = System.Web.HttpContext.Current.Response.Cookies("GUID").Value

Regards,
Mukesh
0
 
LVL 6

Expert Comment

by:badalpatel
ID: 18036479
just add this line before ur line of code..

Response.Cookies("GUID").Expires = DateTime.Now()
Label1.Text = System.Web.HttpContext.Current.Request.Cookies("GUID").Value
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 18036873
Hmmm, you are getting a cookie from the Request, setting the expiration, and then adding it to the Response, and the Request still has the cookie.  What is supposed to remove it from the Request object?

Bob
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Author Comment

by:bhermer
ID: 18037629
Hey Bob, not sure I know what I am doing at all!!,

essentially I have a cookie which carries a GUID. I have a web component that sits on the page acting as a cart monitor, this will read the cookie (it reads a Session Var first and then reads the cookie if not there) and extracts the cart from the DB. Once the customer has paid, the cookie gets deleted and the session var gets reset. The problem being, due to the nature of web components, after I delete the cookie and before the page has finished executing the cart monitor re-renders, it looks at the cookie, which hasn't expired until the page finishs loading, finds it, resets the bl**dy session variable and shows as having items in the cart!

After the page is rendered, and refreshed, the cookie is gone. it is just during code execution that the cookie doesnt seem to be deleted.

badalpatel, that doesnt work, and I am pretty sure that is the same as expiring it one day in the past?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 18037752
Looking at the cart monitor component, can it check the expiration date on the cookie?

Bob
0
 
LVL 5

Expert Comment

by:prajapati84
ID: 18042930
You need to postback once to see the effect of cookie after its expiration. I think u should store value of cookie in any contral (e.g. textbox) and then at the time of expiration of cookie, make the value of that control as blank.

Regards,
Mukesh
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question