0

顧客向けの注文管理システムを開発しています。そして、このシステムでは、IIdentity の代わりに IPrincipal を使用することにしました。

顧客のカートデータをどこに保存すべきか、ずっと考えていました。最後に、Cookie に保存することにしました。

最初の質問は、顧客のカート データをどこに保存すればよいですか? データベース内またはクッキー内?

in 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 オブジェクトのユーザーデータ フィールドにカスタム情報をシリアル化するため。

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

1 に答える 1