简体   繁体   English

多个用户在购物车中看到相同的商品

[英]Multiple user seeing the same items in shopping cart

I have created a shopping cart using session to store the item being added to cart.Upon deployment i found out that the shopping cart is not unique to the user that is other user are getting items added to the cart by other user. 我使用会话创建了一个购物车,用于存储要添加到购物车中的商品。在部署后,我发现该购物车并非用户唯一,其他用户正在获取其他用户添加到购物车中的商品。

I tried to use the login username in the session name to make the session unique to that user but it is not working. 我试图在会话名称中使用登录用户名,以使该会话对该用户唯一,但无法正常工作。

 public class ListOfDataset
 {

        static ListOfDataset()
        {

            string username = ClaimsPrincipal.Current.Identity.Name;

            // If the cart is not in the session, create one and put it there
            // Otherwise, get it from the session
            if (HttpContext.Current.Session[string.Format("ASPNETShoppingCart-{0}", username)] == null)
            {
                Instance = new ListOfDataset();
                Instance.Items = new List<DataSet>();
                HttpContext.Current.Session[string.Format("ASPNETShoppingCart-{0}", username)] = Instance;
            }
            else
            {
                Instance = (ListOfDataset)HttpContext.Current.Session[string.Format("ASPNETShoppingCart-{0}", username)];
            }
        }
}

Updated code,error in else statement-object reference not set and function ChekIfdatasetexist always return false: 更新的代码,未设置else语句-对象引用中的错误,并且函数ChekIfdatasetexist始终返回false:

 public class ListOfDataset
    {



        public static ListOfDataset Instance
        {
            get
            {

                ListOfDataset cart = null;

                if (HttpContext.Current.Session["ASPNETShoppingCart"] == null)
                {
                    cart = new ListOfDataset();

                    cart.Items = new List<DataSet>();

                    HttpContext.Current.Session["ASPNETShoppingCart"] = cart.Items;

                }
                else
                {

                    cart.Items =(List<DataSet> )HttpContext.Current.Session["ASPNETShoppingCart"];
                }

                return cart;
            }
        }

        public List<DataSet> Items { get; private set; }

        public void AddItem(DataSet itemdataset)
        {

            Items.Add(itemdataset);

            HttpContext.Current.Session["ASPNETShoppingCart"] = Items;
        }

        public bool CheckIfDataSetExist(string servicename)
        {
            DataSet DataSetexist = null;

            if (Items != null)
            {
                DataSetexist = Items.Where(i => i.DataSetName == servicename).FirstOrDefault();
            }


            if (DataSetexist != null) return true;

            return false;
        }
    }

There are two issues here. 这里有两个问题。

  1. As someone said in comments you have made the Instance member static, which means it will remain the same application wide and shared among users. 就像有人在评论中说的那样,您已经使Instance成员成为静态成员,这意味着它将在整个应用程序范围内保持相同并在用户之间共享。
  2. Session is not the best place to store per user data cause it will blow up your server memory and wont scale. 会话不是存储每个用户数据的最佳位置,因为它会耗尽服务器内存并不会扩展。 Better use a database to store the data. 更好地使用数据库来存储数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM