zoukankan      html  css  js  c++  java
  • 【转】我碰到Cookie的一个问题

    代码
    CSDN的用户反馈回来一个很诡异的bug,当用户系统的时间不正确的时候,比正确时间快或者慢时,用户就登录不上去,很诡异。解决这个问题花了我不少时间。导致这个问题的原因如下:

    使用 HttpContext.Current.Response.Cookies.Set( 更新一个Cookie后, 会导致 HttpContext.Current.Request.Cookies 中的Cookie的更新全部失效。下面是一个演示这个问题的代码:

    HttpCookie hc1 
    = new HttpCookie("aaa""34556");
    HttpCookie hc2 
    = new HttpCookie("bbb""8888");

    HttpContext.Current.Request.Cookies.Set(hc1);
    Response.Write(HttpContext.Current.Request.Cookies[
    "aaa"].Value);
    Response.Write(
    "\r\n<br />\r\n");

    HttpContext.Current.Response.Cookies.Set(hc2);

    if (HttpContext.Current.Request.Cookies["aaa"!= null)
        Response.Write(HttpContext.Current.Request.Cookies[
    "aaa"].Value);
    else
        Response.Write(
    "null");这个程序执行后,会显示:

    34556 
    null 

    解决方法很简单:

    所有的 HttpContext.Current.Request.Cookies.Set( 代码都放在 HttpContext.Current.Response.Cookies.Set( 代码之后。

    至于为何客户端时间和服务器时间一致时,就没问题,客户端和服务器时间不同步就有问题,那就很怪异了,我一直也没想明白。
    轉自:http:
    //blog.joycode.com/ghj/archive/2010/04/01/115929.joy
  • 相关阅读:
    C#处理json实战
    HDU3994(Folyd + 期望概率)
    POJ1270 Following Orders (拓扑排序)
    HDU 3634 City Planning (离散化)
    HDU4762(JAVA大数)
    POJ3026(BFS + prim)
    POJ1679(次小生成树)
    UVA10487(二分)
    ZOJ 2048(Prim 或者 Kruskal)
    FZU 1856 The Troop (JAVA高精度)
  • 原文地址:https://www.cnblogs.com/gebenhagen/p/1714801.html
Copyright © 2011-2022 走看看