Cookie update expires without changing value

How can I simply update the cookie expiration on every page request without any effect on its value?

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

Or set the maximum value and forget about expiration:

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

Source: https://habr.com/ru/post/1711995/


All Articles