简体   繁体   中英

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. 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? If yes, in which mode,State Server, InProc, Out of Process or SQLServer? Regards

Session state only lasts as long as the user is interacting with your site. Technically a little longer, but once a user navigates away or closes their browser, you should consider the session closed.

Option A: Use a cookie on the user's computer to store items in their shopping list. Store as little information as possible, maybe just the product SKU and the quantity.

Eg: To Create the 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:

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.

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.

Surprisingly when you logged in with User2, you will see the same products and same cart which created by User1.

Because Session cookie sytem works with browser based logic. So as long as you are using same browser and unless the cookie is deleted, the cart will remain even if you switch users.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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