购物车在 IIdentity IPrincipal



我正在为我的客户开发一个订单管理系统。在这个系统中,我决定使用IPrincipal而不是IIdentity。

我一直在考虑应该在哪里存储客户的购物车数据。最后我决定存储在Cookie中。

的第一个问题是:我应该在哪里存储客户购物车数据?在数据库中还是在饼干中?

我认为在饼干中会更快,更有用。我需要你对这个问题的想法。

我试图用饼干存储。我可以将购物车数据添加到 cookie,但是当我尝试将其他产品添加到购物车时,购物车数据正在重置。我想将购物车数据存储在列表中。

我的代码:

1- 我的自定义主体:

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-我的购物车虚拟机

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";
}

我使用会话解决了这个问题。

当用户登录时,我创建了一个会话。并插入所有带有计数的购物车项目。

因此,我可以在布局页面或其他任何地方使用所有数据。

如果有任何其他建议,请随时分享。我在我的项目中使用了cookie或会话。如果我可以将购物车数据添加到 cookie 中会更好。

相关内容

  • 没有找到相关文章