代码如下:            string secretData = "a,b,c";
            string strid = "1";
            FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(1, "newcookie", DateTime.Now, DateTime.Now.AddYears(5), true, secretData);            HttpCookie newUserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(newTicket));
            Response.Cookies.Add(newUserCookie);我设置了保存2年,可是几个小时后就过期了,我不知道哪里出错,希望朋友们给以指点,先谢谢了!

解决方案 »

  1.   

    刚才发布的帖子有点代码忘记了加,CSDN没有办法修改。
    完整代码如下:string secretData = "a,b,c";
    string strid = "1";
    FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(1, "newcookie", DateTime.Now, DateTime.Now.AddYears(5), true, secretData);HttpCookie newUserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(newTicket));
    Response.Cookies.Add(newUserCookie);FormsAuthentication.RedirectFromLoginPage(strid, newTicket.IsPersistent);
      

  2.   

    DateTime.Now.AddYears(5), 汗这不是5年?
      

  3.   

    http://hi.baidu.com/tenss/blog/item/3df06c382f5cc12097ddd89b.html看这篇文章
      

  4.   

    将Cookie信息登录后保存起来。FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,CookieInfo, DateTime.Now, DateTime.Now.AddHours(20),false,UserData); // User data         string encryptedTicket = FormsAuthentication.Encrypt(authTicket); //加密         //   存入Cookie         HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket);         authCookie.Expires = authTicket.Expiration;         Response.Cookies.Add(authCookie); 
      

  5.   

    文章我已经看了,按照里面的操作看不出哪里有问题啊。
    To:kongwei521 
    还望指点一二,谢谢!
      

  6.   

    恩,好东西,学习一下!
    顺便问一下:
    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,CookieInfo, DateTime.Now, DateTime.Now.AddHours(20),false[/color],UserData);

    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,CookieInfo, DateTime.Now, DateTime.Now.AddHours(20),[color=#FF0000]true
    ,UserData);其中的红色部分区别代表什么意义呢?谢谢各位!!1
      

  7.   

    TO:wudi626
    “红色的”代表关闭浏览器是否失效。
      

  8.   


            HttpCookie cookie = (HttpCookie)Request.Cookies[FormsAuthentication.FormsCookieName];
            // Get the cookie's payload == Enrypted FormsAuthenticationTicket 
            string encryptedTicket = cookie.Value;
            // Decrypt the cookie's payload 
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encryptedTicket);
            Response.Write("Ticket Expiry Date: " + ticket.Expiration.ToLocalTime().ToLongDateString() + ticket.Expiration.ToLocalTime().ToLongTimeString() +" <br/>");
            Response.Write("Cookie Expiry Date: " + cookie.Expires.ToLocalTime().ToLongDateString());
    得到如下值:
    Ticket Expiry Date: 2008年6月21日16:17:42 
    Cookie Expiry Date: 0001年1月1日 
      

  9.   

    这样
    string secretData = "a,b,c";
    string strid = "1";
    FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(1, strid, DateTime.Now, DateTime.Now.AddYears(5), true, secretData);HttpCookie newUserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(newTicket));
    Response.Cookies.Add(newUserCookie);
    Response.Redirect(FormsAuthentication.GetRedirectUrl(strid, newTicket.IsPersistent));
    浏览器负责管理 Cookie,而 Cookie 的到期时间和日期可帮助浏览器管理 Cookie 的存储。因此,虽然可以读取 Cookie 的名称和值,但无法读取 Cookie 的到期日期和时间。当浏览器向服务器发送 Cookie 信息时,并不包括有效期信息。(Cookie 的 Expires 属性始终返回值为 0 的日期时间值。) 如果您担心 Cookie 的到期日期,必须重新设置该 Cookie。注意: 
    可以在向浏览器发送 Cookie 之前读取已在 HttpResponse 对象中设置的 Cookie 的 Expires 属性。但是,您无法从返回的 HttpRequest 对象中获取有效期。
     
      

  10.   


    string secretData = "a,b,c";
    string strid = "1";
    FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(1, strid, DateTime.Now, DateTime.Now.AddYears(5), true, secretData);HttpCookie newUserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(newTicket));
    Response.Cookies.Add(newUserCookie);
    Response.Redirect(FormsAuthentication.GetRedirectUrl(strid, newTicket.IsPersistent));Response.Redirect(FormsAuthentication.GetRedirectUrl(strid, newTicket.IsPersistent));
    这样的话关闭浏览器就失效了,请问何解,已经设置isPersistent为true?
      

  11.   

    我看了一下
    Response.Redirect(FormsAuthentication.GetRedirectUrl(strid, newTicket.IsPersistent)); 
    没有在客户端生成Cookie文件,都存储在index.dat文件里。FormsAuthentication.RedirectFromLoginPage(strid, newTicket.IsPersistent);
    有生成Cookie文件,关闭浏览器打开继续有效,但是Cookie值很快过期。
      

  12.   

    HttpCookie newUserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(newTicket));
    newUserCookie.Expires = newTicket.Expiration;//加上这一句
    Response.Cookies.Add(newUserCookie);
      

  13.   

    To:viena 
    加上
    newUserCookie.Expires = newTicket.Expiration;
    这句就可以了,并且在客户端生成了Cookie
    谢谢了!
      

  14.   

    呵呵 VIENA也很厉害呀,学习!