【问题标题】:Session visible on other computers?会话在其他计算机上可见?
【发布时间】:2013-08-20 16:22:03
【问题描述】:

我有一个奇怪的问题,基本上我有一个使用会话的购物车。当我使用 IIS7 部署站点时,一切看起来都很好。我在一台电脑上将产品添加到会话中,它会显示在我的购物篮中。当我从另一台电脑访问该网站时,篮子里有这个项目!!??

我的理解是每个用户浏览器的会话实例都是唯一的,这是正确的吗?如果是这样,我是怎么做到的?我知道这可能很愚蠢,但我无法弄清楚,非常感谢任何帮助!

我的会话购物车代码如下

#region Singleton Implementation

        public static readonly ShoppingCart Instance;
        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["sCart"] == null)
            {
                Instance = new ShoppingCart();
                Instance.Items = new List<CartItem>();
                HttpContext.Current.Session["sCart"] = Instance;
            }
            else
            {
                Instance = (ShoppingCart)HttpContext.Current.Session["sCart"];
            }

        }

        protected ShoppingCart() { }

        #endregion

【问题讨论】:

标签: c# asp.net session session-variables


【解决方案1】:

您正在存储对单个全局 ShoppingCart 的单个静态引用。
这是一个可怕的想法。

每当你写ShoppingCart.Instance时,它总是返回你在静态构造函数中设置的原始值。

您需要摆脱单例并始终使用会话。

【讨论】:

    【解决方案2】:

    是因为public static readonly ShoppingCart Instance;

    由于静态(适用于应用程序级别),该实例始终为每个人返回相同的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      • 2022-11-30
      • 2015-08-27
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      相关资源
      最近更新 更多