私は顧客の注文管理システムを削除しています。このシステムでは、IIdentityではなくIPrincipalを使用することにしました。ショッピングカートをIIdentityに登録するIPrincipal
私は顧客のカートのデータをどこに保存するかについて長い間考えていました。最後に私はCookieに保存することに決めました。
私の最初の質問は次のとおりです。お客様のカートのデータはどこに保存する必要がありますか?データベース内またはIn 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でクッキーを設定する:
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";
}