0

I am delevoping an order management system for my customers. And in this system, I decided to use IPrincipal instead of IIdentity.

I have been thinking for a long time about where I should store customer's cart data. Finally I decided to store in Cookie.

My first question is: Where should I store customer cart data ? In database or In cookie ?

I think in cookie will be faster and more useful. I need your ideas on this subject.

I tried to store with cookie. I can add shopping cart data to cookie but when I try to add another product to cart, shopping cart data is resetting. I want to store shopping cart data in list.

My codes:

1- My 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 - For serializing custom information into userdata field in FormsAuthenticationTicket object.

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- My login method - setting up a cookie with custom informatin:

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 Reading cookie and replacing HttpContext.User object, this is done by overriding 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- My Cart 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- Add to cart method

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";
}
senjizu
  • 103
  • 12

1 Answers1

0

I solved this problem using session.

When user log in, I created a session. And inserted all of cart items with counts.

So, I can use all of data in layout page or anywhere else.

If there is any other suggestion, please do not hesitate to share. I am using either cookie or session in my project. It would be better if I could add cart data into the cookie.

senjizu
  • 103
  • 12