【问题标题】:cookie does not addingcookie 不添加
【发布时间】:2012-07-10 11:58:27
【问题描述】:

我正在服务器上添加 cookie:

private void AddCookie(int id)
{
    HttpCookie cookie = new HttpCookie("wmpayment");
    cookie.Value = id.ToString();
    cookie.Expires = DateTime.Now.AddDays(2);
    this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
}

但是当我从请求中读取 cookie - cookie.Expire 等于日期 01.01.0001

public static int WMPendingOrder
{
    get
    {
        var cookie = HttpContext.Current.Request.Cookies["wmpayment"];
        int id = 0;
        DateTime exp;

        if (cookie != null)
        {
            DateTime.TryParse(cookie.Expires.ToString(), out exp);
            if (DateTime.Now < exp)
                int.TryParse(cookie.Value, out id);
        }
        return id;
    }
}

日志:COOKIE.Name:wmpayment COOKIE.Value:0 COOKIE.Expire:01.01.0001 0:00:00 我不明白这是什么问题。

【问题讨论】:

  • 这条线有问题.."比我读到的-expires = 01.01.0001:"..请更改它..很难理解你想要什么..
  • 我发现本教程很有帮助,您可以将其视为替代方法:code-inside.de/blog-in/2010/10/19/…
  • 我解决了这个问题如下。只是简单地重置 cookie 的值 - 将 orderId 设置为“0”。但非常有趣的是为什么浏览器在服务器上删除 cookie 后不删除它。

标签: asp.net .net asp.net-mvc-3 cookies


【解决方案1】:

所以基本上有两条信息需要你坚持。 id 和到期日期。如何将到期日期存储在单独的 cookie 中:

private void AddCookie(int id) 
{ 
    HttpCookie cookie = new HttpCookie("wmpayment"); 
    cookie.Value = id.ToString(); 
    cookie.Expires = DateTime.Now.AddDays(2); 
    this.ControllerContext.HttpContext.Response.Cookies.Add(cookie); 

    HttpCookie cookie = new HttpCookie("wmpaymentexpire"); 
    cookie.Value = DateTime.Now.AddDays(2).ToString(); 
    cookie.Expires = DateTime.Now.AddDays(2); 
    this.ControllerContext.HttpContext.Response.Cookies.Add(cookie); 
}

所以要检查 cookie wmpayment 的过期日期,您需要读取 cookie wmpaymentexpire 的值。

【讨论】:

  • 我不认为这是一个好的变体。我想了解问题。
  • 如果您不喜欢我的建议,那就足够了。但正如 Jonathan Rupp 在他的回答中所写,客户端不会将其 cookie 的到期日期发送回服务器。因此,您可以在服务器上设置 cookie 的到期日期并将其发送给客户端。但是客户端不发回,所以你不能在服务器上读取它。只有 cookie 的名称和值从客户端发送到服务器。
  • 非常感谢。我不知道。好吧,你能告诉我为什么删除服务器上的cookie后它存在于客户端吗?
  • 请参阅here 了解有关如何使客户端删除 cookie 的详细信息。
【解决方案2】:

您可以使用此代码创建 cookie:

            FormsAuthenticationTicket tkt;
            string cookiestr;
            HttpCookie ck;

            tkt = new FormsAuthenticationTicket(1, UsrNm, DateTime.Now,
      DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "Issue Ticket");
            cookiestr = FormsAuthentication.Encrypt(tkt);

            ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
            if (chkPersistCookie.Checked)
                ck.Expires = tkt.Expiration;
            ck.Path = FormsAuthentication.FormsCookiePath;
            Response.Cookies.Add(ck);

            string strRedirect;
            strRedirect = Request["ReturnUrl"];
            if (strRedirect == null)
                strRedirect = "~/default.aspx";
            Response.Redirect(strRedirect, true);

*注意:*为FormsAuthenticationTicket添加程序集using System.Web.Security

【讨论】:

    【解决方案3】:

    当 cookie 被提交回服务器时,它们不包含“过期”选项,因此不会填充 exp,因此它保持其默认值 DateTIme.MinValue。因此,DateTime.Now &lt; exp 永远不会为真,所以int.TryParse(cookie.Value, out id) 永远不会运行,所以 id 保持它的默认值,0

    试试这个:

    public static int WMPendingOrder
    {
        get
        {
            var cookie = HttpContext.Current.Request.Cookies["wmpayment"];
            int id = 0;
    
            if (cookie != null)
            {
                int.TryParse(cookie.Value, out id);
            }
            return id;
        }
    }
    

    您无需在服务器端检查过期的 cookie - 如果它们过期,客户端将不会发送它们。

    【讨论】:

    • 在提交 webmoney 商家请求之前,我仍保留在 cookie 订单 ID 中。如果支付订单成功,我删除 cookie,如果没有,我将显示锚点,并显示用户有待处理支付订单的消息。所以我需要检查 Expire cookie
    • @user571874:如果“Expires”值很重要,您需要将其存储为 cookie 的值(编码到同一个 cookie 中,或者按照@user1429080 的建议存储在单独的 cookie 中) .客户端不会将 cookie 过期信息发送回服务器 - 只是名称和值。
    猜你喜欢
    • 1970-01-01
    • 2016-09-20
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多