我有一个电子商务网站。我使用饼干。但是一些客户向我报告了关于 cookie 的错误。用户将产品添加到购物车后,我创建了一个 cookie。单击立即购买按钮后,cookie 将在 mycart.aspx 页面中过期。你可以在所属图片中看到我的历史。
我将产品添加到购物车,一切正常 =>
如您所见,过期日期也可以。
在我点击立即购买按钮后,重定向到 mycart.aspx 但它说,购物车是空的。
当我检查 cookie 过期时间时,我看到了这个视图。Cookie 已过期。
这是我的 cookie 集和获取代码:
public bool SetCookie(string cookieName, object cookieValue, DateTime expirationDate)
{
try
{
//Eğer atanmak istenen cookie daha önce oluşturuldu ise, içini boşaltıyoruz.
if (HttpContext.Current.Request.Cookies[cookieName] != null)
{
HttpContext.Current.Request.Cookies.Remove(cookieName);
}
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, cookieValue);
byte[] bArr = ms.ToArray();
MemoryStream objStream = new MemoryStream();
//stream nesnesini sıkıştır
DeflateStream objZS = new DeflateStream(objStream, CompressionMode.Compress);
objZS.Write(bArr, 0, bArr.Length);
objZS.Flush();
objZS.Close();
byte[] bytes = objStream.ToArray();
string sCookieVal = Convert.ToBase64String(bytes);
HttpCookie cook = new HttpCookie(cookieName);
cook.Value = sCookieVal;
cook.Expires = expirationDate;
HttpContext.Current.Response.Cookies.Add(cook);
return true;
}
catch (Exception exc)
{
throw exc;
}
}
public object GetCookie(string cookieName)
{
if (cookieName == null) throw new ArgumentNullException("cookieName");
try
{
if (HttpContext.Current.Request.Cookies[cookieName] != null)
{
if (HttpContext.Current.Request.Cookies[cookieName].Value != "")
{
byte[] bytCook =
Convert.FromBase64String(HttpContext.Current.Request.Cookies[cookieName].Value);
MemoryStream inMs = new MemoryStream(bytCook);
inMs.Seek(0, 0);
DeflateStream zipStream = new DeflateStream(inMs, CompressionMode.Decompress, true);
byte[] bArr = ReadFullStream(zipStream);
zipStream.Flush();
zipStream.Close();
MemoryStream outMs = new MemoryStream(bArr);
outMs.Seek(0, 0);
BinaryFormatter bf = new BinaryFormatter();
return bf.Deserialize(outMs, null);
}
} return null;
}
catch (Exception ex)
{
throw (ex);
}
}
你知道会发生什么吗?