[英]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. 这里有两个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.