Thursday 8 July, 2010

CHECK FOR COOKIE ACCEPTANCE

First time a user visits the site, there will be a double re-direct, but if they keep that cookie around (for 1000 years), that'll only ever happen the first time. Any user who does not accept cookies will get stuck on the CookieCheck.aspx page without a redirect. I use the first part of this on a master page, making the cookie check present for every page that inherits it

  1. // on the master page or default page
  2. protected void Page_Load(object sender, EventArgs e)
  3. {
  4. // check for a cookie indicating cookie acceptance
  5. HttpCookie tokenCookie = Request.Cookies["AcceptsCookies"];
  6. if (tokenCookie == null)
  7. {
  8. tokenCookie = new HttpCookie("AcceptsCookies", "true");
  9. tokenCookie.Expires = DateTime.Now.AddYears(1000);
  10. Response.Cookies.Add(tokenCookie);
  11. Response.Redirect("~/CheckCookies.aspx");
  12. }
  13. }
  14. // on the CheckCookies.aspx page
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. HttpCookie tokenCookie = Request.Cookies["AcceptsCookies"];
  18. if (tokenCookie != null)
  19. Response.Redirect("Default.aspx");
  20. }

No comments:

Post a Comment