动态设置Cookies过期时间

              DateTime dt = DateTime.Now;
                switch (dropSaveDate.SelectedValue)//dropSaveDate为
                {
                    case "2小时":
                        dt = DateTime.Now.AddMinutes(120);//二小时过期
                        break;
                    case"一天":
                        dt = DateTime.Now.AddDays(1);
                        break;
                    case"一周":
                        dt = DateTime.Now.AddDays(7);
                        break;
                    case"一月":
                        dt = DateTime.Now.AddMonths(1);
                        break;
                    default:
                        break;
                }
                // 创建身份验证票证
                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket (                                                              

                                       1, // version
                                       gUserId, // user name
                                       DateTime.Now, // 开始时间
                                       dt,// 过期时间为一   //天:DateTime.Now.AddDays(1)
                                       true, // 持久性,若为True,需设置,Cookies的Expires属性,若为False,则关闭浏览器就会失效
                                        ""// User data

);             

    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                // 创建Cookie
    HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
    authCookie.Expires = authTicket.Expiration;

    Response.Cookies.Add(authCookie);

posted @ 2008-04-18 11:03  网络金领  阅读(665)  评论(0编辑  收藏  举报