Tuesday, June 24, 2008

Set Cookie expiry time

HttpCookie cookie = new HttpCookie("CookieName");
cookie.Value = "My important stuff to remember";
cookie.Expires = DateTime.Now.AddMinutes(30);
// Add the cookie to the out going Http Response
Response.Cookies.Add(cookie);
// This is all being done on the server so the following line is
// needed to actuall send the cookie
Response.Redirect(Page.Request.Path);

HttpCookie cookie = Request.Cookie["CookieName"];
// set cookie to expire 10 years ago
cookie.Expires = DateTime.Now.AddYears(-10);
Response.Cookies.Add(cookie);

Response.Redirect(Page.Request.Path);

0 comments: