Cookie得到自己的到期

时间:2012-11-23 09:57:33

标签: c# .net cookies session-cookies

我有一个电子商务网站。我用饼干。但有些客户报告我有关cookie的错误。我在用户将产品添加到购物车后创建了一个cookie。点击立即购买按钮后,cookie在mycart.aspx页面中显示。你可以在属于pics中看到我的历史。

我将产品添加到购物车,一切正常=> enter image description here

如您所见,过期日期也可以。

enter image description here

点击“立即购买”按钮后,重定向到mycart.aspx,但是说,购物车是空的。

enter image description here

当我检查cookie过期时间时,我看到了这个视图。 Cookie过期了。

enter image description here

这是我的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);
    }
}

你知道发生了什么吗?

0 个答案:

没有答案