简体   繁体   English

使用 asp.net 将用户购物车保存在 sessionState 中以使其持久化是否专业?

[英]Is it professional to save Users shopping cart in a sessionState for making it persistent using asp.net?

I want to make a persistent shopping cart for a MVC application, in other words if a user add some products to his/her cart when logged in and leaves the application without making an order or removing the item from his/her cart.我想为 MVC 应用程序制作一个持久的购物车,换句话说,如果用户在登录时将一些产品添加到他/她的购物车并离开应用程序而没有下订单或从他/她的购物车中删除商品。 I want to let users have their added items in their shopping cart until some certain amount of time lets say one month, ordering or removing the items from his/her cart manually.我想让用户在他们的购物车中添加他们添加的商品,直到某个特定的时间,比如一个月,手动订购或从他/她的购物车中删除商品。 Is it a good idea to save their cart in asp.net sessionState?将他们的购物车保存在 asp.net sessionState 中是个好主意吗? If yes, in which mode,State Server, InProc, Out of Process or SQLServer?如果是,在哪种模式下,State Server、InProc、Out of Process 或 SQLServer? Regards问候

Session state only lasts as long as the user is interacting with your site. Session state 仅在用户与您的网站进行交互时持续。 Technically a little longer, but once a user navigates away or closes their browser, you should consider the session closed.从技术上讲要长一点,但是一旦用户离开或关闭浏览器,您应该认为 session 已关闭。

Option A: Use a cookie on the user's computer to store items in their shopping list.选项 A:使用用户计算机上的 cookie 将商品存储在他们的购物清单中。 Store as little information as possible, maybe just the product SKU and the quantity.尽可能少地存储信息,可能只存储产品 SKU 和数量。

Eg: To Create the cookie例如:创建 cookie

HttpCookie cartCookie = new HttpCookie("TheCart");
DateTime now = DateTime.Now;

// Possibly a serialised string of the shopping cart object (ensure no sensitive info though)
cartCookie.Value = shoppingCartData;

// Set when the cookie should expire. 
cartCookie.Expires = now.AddMonths(1);

// Add the cookie too the response
Response.Cookies.Add(cartCookie);

To Read the Cookie:读取 Cookie:

HttpCookie cartCookie = new HttpCookie("TheCart");
cartCookie = Request.Cookies["TheCart"];

Option B: If your user is logged in, then persist the cart state in a data store - for example, relational, on disk or in a NoSQL DB.选项 B:如果您的用户已登录,则将购物车 state 保存在数据存储中 - 例如,关系、磁盘或 NoSQL 数据库中。

Well I Wouldn't do that.好吧,我不会那样做。 Let us speak with a scenario:让我们用一个场景来说话:

You logged in with User1 and add a product in cart then logged off.您使用 User1 登录并在购物车中添加产品,然后注销。

Surprisingly when you logged in with User2, you will see the same products and same cart which created by User1.令人惊讶的是,当您使用 User2 登录时,您会看到与 User1 创建的相同的产品和相同的购物车。

Because Session cookie sytem works with browser based logic.因为 Session cookie 系统使用基于浏览器的逻辑。 So as long as you are using same browser and unless the cookie is deleted, the cart will remain even if you switch users.因此,只要您使用相同的浏览器并且除非删除 cookie,否则即使您切换用户,购物车也会保留。

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

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