2017-04-11 65 views
0

我正在爲我的客戶提供訂單管理系統。在這個系統中,我決定使用IPrincipal而不是IIdentity。IIdentity購物車IPrincipal

我一直在想我應該在哪裏存儲客戶的購物車數據。最後我決定存儲在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對象。

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-我的登錄方法 - 建立與自定義informatin一個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中讀餅乾和更換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

我使用會話解決了這個問題。

當用戶登錄時,我創建了一個會話。並插入所有的購物車項目。

所以,我可以使用佈局頁面或其他任何地方的所有數據。

如果有其他建議,請不要猶豫分享。我在我的項目中使用Cookie或會話。如果我可以將購物車數據添加到Cookie中會更好。