更新cookie过期而不会更改值

时间:2009-07-07 11:47:56

标签: asp.net cookies

如何在不对每个页面请求产生任何影响的情况下更新cookie的到期时间?

2 个答案:

答案 0 :(得分:2)

 'update the User cookie expiration time on every page load
    Dim cookieName As String = ConfigKeys.UserCookieName
    Dim cookieExpr As String = ConfigKeys.CookieExpiration.ToString

    '--get the cookies from request object
    Dim userCookie As HttpCookie = HttpContext.Current.Request.Cookies(cookieName.ToUpper())
    '--set the expiry date
    userCookie.Expires = DateTime.Now.AddMinutes(Integer.Parse(cookieExpr))
    '--add the updated cookies back to Response object
    HttpContext.Current.Response.Cookies.Add(userCookie)

答案 1 :(得分:2)

HttpContext.Current.Response.Cookies["MyCookie"].Expires =
    DateTime.Now.AddDays(1)

或者将其设置为最大值并忘记过期时间:

HttpContext.Current.Response.Cookies["MyCookie"].Expires =
    DateTime.MaxValue