views:

463

answers:

2

How can i simply update the expiry of a cookie on each page request without having any impact on its value ?

+2  A: 
 '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)
Vikram
+1  A: 
HttpContext.Current.Response.Cookies["MyCookie"].Expires =
    DateTime.Now.AddDays(1)

Or set it to maximum and forget about the expiration:

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