Posted By

Anthony on 08/29/08


Tagged

test cookie acceptance


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

umang_nine
emregulcan


Check for Cookie Acceptance


 / Published in: C#
 

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.  
  15. // on the CheckCookies.aspx page
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. HttpCookie tokenCookie = Request.Cookies["AcceptsCookies"];
  19. if (tokenCookie != null)
  20. Response.Redirect("Default.aspx");
  21. }

Report this snippet  

You need to login to post a comment.