Return to Snippet

Revision: 8046
at August 29, 2008 16:04 by Anthony


Initial Code
// on the master page or default page
protected void Page_Load(object sender, EventArgs e)
{
    // check for a cookie indicating cookie acceptance
    HttpCookie tokenCookie = Request.Cookies["AcceptsCookies"];
    if (tokenCookie == null)
    {
        tokenCookie = new HttpCookie("AcceptsCookies", "true");
        tokenCookie.Expires = DateTime.Now.AddYears(1000);
        Response.Cookies.Add(tokenCookie);
        Response.Redirect("~/CheckCookies.aspx");
    }
}

// on the CheckCookies.aspx page
protected void Page_Load(object sender, EventArgs e)
{
    HttpCookie tokenCookie = Request.Cookies["AcceptsCookies"];
    if (tokenCookie != null)
        Response.Redirect("Default.aspx");
}

Initial URL


Initial Description
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.

Initial Title
Check for Cookie Acceptance

Initial Tags


Initial Language
C#