Display a “We Use Cookies” warning in ASP

The majority of websites around the world are displaying a cookie warning as soon as you open them, because of the “Cookie Law”. The Cookie Law is a piece of privacy legislation that requires websites to get consent from visitors to store or retrieve any information on a computer, smartphone or tablet. In this article I will give you a step by step guide on how to show you how to display a “We Use Cookies” warning in ASP

Overview

The “Cookie Law” was designed to protect privacy, by displaying to netizens how information about them is collected and used online, and give them a choice to allow it or not. More info about it here. Basically we need to let it know the user if our website is using cookies.

Getting Started

1.  Open your ASP website/App with Visual Studio. Before setting a cookie, first you need to show a message to enable the cookie. On your project, add this code:

protected void Page_Load(object sender, EventArgs e)
   {
       if (Request.Cookies["check"] != null)
      {

           this.cookiesMsg.Visible = false;

           if (Response.Cookies["check"].Value == "Yes")
           {
               Response.Cookies["check"].Value = "No";
               Response.Cookies["Cookies"]["FirstName"] = "John";
                Response.Cookies["Cookies"]["MiddleName"] = "Ryan";
               Response.Cookies["Cookies"]["LastName"] = "Clinton";
               Response.Cookies["Cookies"].Expires = DateTime.Now.AddDays(1);
           }
       }
       else {
           this.cookiesMsg.Visible = true;
       }
   }

   void PastCookies() {
       Response.Cookies["check"].Value = "Yes";
       Response.Cookies["check"].Expires = DateTime.Now.AddDays(1);
       Response.Redirect(Request.RawUrl);
   }

   void NoCookies()
   {
       Response.Cookies["check"].Value = "No";
       Response.Cookies["check"].Expires = DateTime.Now.AddDays(1);
       Response.Redirect(Request.RawUrl);
   }

2

2. Now, on the main page of your ASP site, add the following code to display the warning:

22

3. The warning will display like this on your site:

1

4. And that’s it! Now your site displays a “cookie warning” on load.

With this we make a warning and let the user decides if he wants to use the cookies in asp.net. Also, you can use JavaScript or jQuery to enable the cookies and make the warning.

This concludes Display a “We Use Cookies” warning in ASP

Looking for quality Windows Hosting? Look no further than Arvixe Web Hosting!

Happy Hosting!

Rodolfo Hernandez

Tags: , , , , , , , , , , , , , , , | Posted under ASP .NET 3.5, ASP .NET 4.0, DotNet/Windows Hosting | RSS 2.0

Author Spotlight

Rodolfo Hernandez

I like photography and reading books. Currently working for Arvixe as Elgg Community Liaison. Elgg Security Expert Web Security Expert CEO of UDP SW Social Web

2 Comments on Display a “We Use Cookies” warning in ASP

  1. Hafiz says:

    What will happen if I clear the browing history from may mobile?my cookies will be also deleted from their surver or not?

Leave a Reply

Your email address will not be published. Required fields are marked *