我正在为我的客户设计订单管理系统。在这个系统中,我决定使用IPrincipal而不是IIdentity。
我一直在思考应该在哪里存储客户的购物车数据。最后我决定存储在Cookie中。
我的第一个问题是:我应该在哪里存储客户购物车数据?在数据库中还是在Cookie中?
我认为在cookie中会更快,更有用。我需要你关于这个主题的想法。
我尝试用cookie存储。我可以将购物车数据添加到cookie,但是当我尝试将另一个产品添加到购物车时,购物车数据正在重置。我想将购物车数据存储在列表中。
我的代码:
1-我的CustomPrincipal:
public class CustomPrincipal:IPrincipal
{
public IIdentity Identity{ get; private set; }
public bool IsInRole(string Role) { return false;}
public CustomPrincipal(string UserName){
this.Identity = new GenericIdentity(UserName);
}
public int UserId { get; set; }
public string UserName { get; set; }
public int RoleId { get; set; }
public bool IsAdmin { get; set; }
public List<Models.DTO.CartDTO.CartVM> Cart { get; set; }
}
2- CustomPrincipalSerializeModel - 用于将自定义信息序列化为FormsAuthenticationTicket对象中的userdata字段。
public class CustomPrincipalSerializeModel
{
public int Id { get; set; }
public string UserName { get; set; }
public int RoleId { get; set; }
public bool IsAdmin { get; set; }
public List<Models.DTO.CartDTO.CartVM> Cart { get; set; }
}
3-我的登录方法 - 使用自定义信息设置cookie:
if (rplogin.Any(x => x.UserName == model.UserName && x.Password == model.Password && x.IsDeleted == false))
{
var member = rplogin.FirstOrDefault(x => x.UserName == model.UserName);
member.LastLoginDate = DateTime.Now;
rplogin.SaveChanges();
Models.DTO.Security.CustomPrincipalSerializeModel serializeModel = new Models.DTO.Security.CustomPrincipalSerializeModel();
serializeModel.Id = member.Id;
serializeModel.UserName = member.UserName;
serializeModel.RoleId = member.RoleId;
serializeModel.IsAdmin = member.IsAdmin;
serializeModel.Cart = new List<Models.DTO.CartDTO.CartVM>();
JavaScriptSerializer serializer = new JavaScriptSerializer();
string userData = serializer.Serialize(serializeModel);
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
model.UserName,
DateTime.Now,
DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
false,
userData
);
string encTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)
{
HttpOnly = true
};
Response.Cookies.Add(faCookie);
return RedirectToAction("Index", "Management");
}
else
{
ViewBag.IsLogged = false;
}
}
return View();
4- Global.asax.cs读取cookie并替换HttpContext.User对象,这是通过重写PostAuthenticateRequest来完成的
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
JavaScriptSerializer serializer = new JavaScriptSerializer();
CustomPrincipalSerializeModel serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(authTicket.UserData);
CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
newUser.UserId = serializeModel.Id;
newUser.RoleId = serializeModel.RoleId;
newUser.UserName = serializeModel.UserName;
newUser.IsAdmin = serializeModel.IsAdmin;
newUser.Cart = serializeModel.Cart;
HttpContext.Current.User = newUser;
}
}
5-我的购物车VM
public class CartVM
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public int VariationId { get; set; }
public string VariationName { get; set; }
public int ColorId { get; set; }
public string ColorName { get; set; }
public decimal Discount { get; set; }
public decimal Amount { get; set; }
}
6-添加到购物车方法
public string AddToCart(string prdctname, int vrtnId, int clrId, int qntty)
{
Models.DTO.CartDTO.CartVM cartdto = new Models.DTO.CartDTO.CartVM();
cartdto.ColorId = clrId;
cartdto.ProductName = prdctname;
cartdto.VariationId = vrtnId;
User.Cart.Add(cartdto);
return "Added to cart";
}
答案 0 :(得分:0)
我使用session解决了这个问题。
当用户登录时,我创建了一个会话。并插入所有带有计数的购物车项目。
因此,我可以在布局页面或其他任何地方使用所有数据。
如果有任何其他建议,请不要犹豫,分享。我在我的项目中使用cookie或会话。如果我可以将购物车数据添加到cookie中会更好。