locked
session id not unique RRS feed

  • Question

  • User65893105 posted

    Ive created a simple shopping cart on my site but something weird is happening.  The session is not unique for each user of the site.  What seesm to be happeing is that If I add something to the shopping cart from one instance of my browser then say I load the site up on an ipad and add a complet ely separate set of items they all end up in the same session

    heres my code

    // The static constructor is called as soon as the class is loaded into memory
        static ShoppingCart() {
            // If the cart is not in the session, create one and put it there
            // Otherwise, get it from the session
            if (HttpContext.Current.Session["ShoppingCart"] == null) {
                Instance = new ShoppingCart();
                Instance.Items = new List<CartItem>();
                HttpContext.Current.Session["ShoppingCart"] = Instance;
            } else {
                Instance = (ShoppingCart)HttpContext.Current.Session["ShoppingCart"];
            }
        }

    so I end up with kitems in the cart that have been added by many different users, can anyone see where Ive gone wrong ?  How do I keep the sessions unique to each user ?

    Thursday, July 9, 2015 11:52 AM

Answers

  • User-484054684 posted

    I believe, it is due to the "static" keyword there.

    static objects scope will be "per" application. o

    If you need objects "per" user - you would create normal (non-static) objects.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 9, 2015 11:59 AM
  • User753101303 posted

    Hi,

    Check how is declared Instance which appears to be static as well.

    As already pointed session are unique but a common issue is to use static *data* (code is ok) as static data are shared by all users (it's confusing for Windows dev as for a Windows app it works because a user has his own unique application instance while for a web application, you have to keep in mind that you deal with a SINGLE application that is used by MULTIPLE users at the same time).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 9, 2015 12:32 PM

All replies

  • User-484054684 posted

    I believe, it is due to the "static" keyword there.

    static objects scope will be "per" application. o

    If you need objects "per" user - you would create normal (non-static) objects.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 9, 2015 11:59 AM
  • User753101303 posted

    Hi,

    Check how is declared Instance which appears to be static as well.

    As already pointed session are unique but a common issue is to use static *data* (code is ok) as static data are shared by all users (it's confusing for Windows dev as for a Windows app it works because a user has his own unique application instance while for a web application, you have to keep in mind that you deal with a SINGLE application that is used by MULTIPLE users at the same time).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 9, 2015 12:32 PM