locked
Destroy Session RRS feed

  • Question

  • User1445074224 posted

    I have an e-commerce site which stores data about items added to a cart in a SQL table with a Session ID. That cart is then pulled out (using a WHERE SESSION ID=) logic and made into an order. Thats it the order process is complete.

    My problem is that, unless the user closes and reopens his/her browser, the session id remains the same, This means that, if the user goes back to the "store" and begins a new order, the items are added to the last order, which still exists: the new items have the same session id as the last order item and are all thrown into the new cart together.

    I have a button which handles the last stage in the order process (taking the user to a "Thank you foryour order" page). I'd like to use some C# in this button's event handler to destroy the session_id without requiring the user to close the browser.

    I also have a quirk with this same issue. If the user has more than one instanve of his/her browser open at the same time (even if one is viewing a completely different web site), then closing and reopening the browser for the e-commerce site doesn't destroy the old session variable. It is as though the second instance inherits the same session id from the first instance which holds it there until ALL instance of the web browser are closed.

    So, can anyone help with code for destroying this session id without closing the browser AND in the event that another instance of the browser is open holding the session id static?

    Thanks.

    Monday, October 22, 2012 8:40 AM

Answers

  • User1779161005 posted

    Don't rely upon ASP.NET's session ID and use your own. You can then store that in Session. When you use Session.Abandon() your custom ID will be purged (as desired). So the next time they come in (regardless if it's the same ASP.NET sesison ID, your custom ID will be regenerated.

    I could show you how to hack with ASP.NET's session, but I'd suggest that it's best to leave it be.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2012 8:46 AM
  • User1779161005 posted

    Guid.NewGuid() :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2012 9:40 AM
  • User1779161005 posted

    Well... you're building a shopping cart right? So something like this:

    if (Session["MyCartID"] == null) {

        Session["MyCartID"] = Guid.NewGuid();

    }

    AddItemToCart(item, Session["MyCartID"].ToString());

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2012 11:41 AM

All replies

  • User1779161005 posted

    Don't rely upon ASP.NET's session ID and use your own. You can then store that in Session. When you use Session.Abandon() your custom ID will be purged (as desired). So the next time they come in (regardless if it's the same ASP.NET sesison ID, your custom ID will be regenerated.

    I could show you how to hack with ASP.NET's session, but I'd suggest that it's best to leave it be.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2012 8:46 AM
  • User1445074224 posted

    Thank you for your response.

    That sound like exactly what I need but I've never used a "custom" session id before. How do I go about setting and retirieving that? C#.

    Monday, October 22, 2012 9:09 AM
  • User1779161005 posted

    Guid.NewGuid() :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2012 9:40 AM
  • User1445074224 posted

    Can you give me a little more detail. Forgive my ignorance but, while this may be childs' play to you, it means nothing to me. I presume you are "setting" an id here. Yes? No? And how to use it later, say to assign to a string variable? 

    Monday, October 22, 2012 10:02 AM
  • User1779161005 posted

    Well... you're building a shopping cart right? So something like this:

    if (Session["MyCartID"] == null) {

        Session["MyCartID"] = Guid.NewGuid();

    }

    AddItemToCart(item, Session["MyCartID"].ToString());

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2012 11:41 AM
  • User1445074224 posted

    BrockAllen,

    Thanks for your help, here.

    Bit of a nightmare handling the difference between the .NET session id and the GUID session id: the former being already a string value and the latter not being. Lots of casting in other code.

    Got it working now, though.

    Thanks.

    Tuesday, October 23, 2012 5:12 AM