Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to use Cookies in ASP.NET

Author: Nikki Smith
Download Source Code : 1280_Cookies.zip

Cookie provides you the way to store user specific information on client side. I will explain cookies in this article with an example.

 

A cookie is a small piece of text that is stored on client’s browser or on client’s hard disk. This text contains user information for the web site to remember that user. The information is stored from the server side when user visits website and sent back to server when user revisits that website. The information is only stored if the client’s browser sets the permission to store cookies. Mostly this information is about user preferences for that website.

Cookies are set for the entire website so these are not for a single page in the website. Single cookie information is exchanged between server and browser for every page in the website however, for every website, browser stores cookies separately.
 
Cookies are stored on client side so server memory is not used and server has to do less work to remember every individual user preferences because every client’s browser stores and arrange all cookies for client.
 
Although cookies have some advantages but there are many limitations for Cookies. Cookies are not a secure way to store information as it stores information in plain text. All browsers limit the number of cookies for websites and also limit the amount of data that can be stored. Another limitation is that we cannot set cookies if user disabled the Cookies in browser.
 
Creating cookies and retrieving cookies information is simple in ASP.NET.  You can also download complete source code.
 
  1. Create a new Web Site in Visual Studio 2010 either in Visual Basic or Visual C#.
  2. Add a Web Form in the Web Site.
  3. Write code below in aspx page

    <p>Create and retrieve cookies</p>
    <asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
    <br />
    <asp:TextBox ID="txtboxName" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="lblAge" runat="server" Text="Age:"></asp:Label>
    <br />
    <asp:TextBox ID="txtboxAge" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="ButtonCreateCookies" runat="server" Text="Create Cookies"
                onclick="ButtonCreateCookies_Click" />
    <br />
    <asp:Label ID="lblCreateCookies" runat="server" ></asp:Label>
    <br />
    <asp:Button ID="ButtonRetrieveCookies" runat="server" Text="Retrieve Cookies"
                onclick="ButtonRetrieveCookies_Click" />
    <br />
    <asp:Label ID="lblRetrieveCookies" runat="server" ></asp:Label>

     
  4.  Write ButtonCreateCookies event in code behind file

    Visual Basic
     
    Protected Sub ButtonCreateCookies_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonCreateCookies.Click
        Dim name As String = txtboxName.Text
        Dim cookie As New HttpCookie("Name", name)
        cookie.Expires = DateTime.Now.AddDays(7)
        Response.Cookies.Add(cookie)
     
        Dim age As String = txtboxAge.Text
        Response.Cookies("Age").Value = age
        Response.Cookies("Age").Expires = DateTime.Now.AddDays(7)
     
        lblCreateCookies.Text = "Cookies are stored"
    End Sub
     
    Visual C#
     
    protected void ButtonCreateCookies_Click(object sender, EventArgs e) {
        string name = txtboxName.Text;
        HttpCookie cookie = new HttpCookie("Name", name);
        cookie.Expires = DateTime.Now.AddDays(7);
        Response.Cookies.Add(cookie);
           
        string age = txtboxAge.Text;
        Response.Cookies["Age"].Value = age;
        Response.Cookies["Age"].Expires = DateTime.Now.AddDays(7);
     
        lblCreateCookies.Text = "Cookies are stored";
    }

     
  5.  Now write ButtonRetrieveCookies event in code behind file

    Visual Basic
     
    Protected Sub ButtonRetrieveCookies_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonRetrieveCookies.Click
        If Request.Cookies("Name") IsNot Nothing And Request.Cookies("Age") IsNot Nothing Then
            Dim cookie As HttpCookie = Request.Cookies("Name")
            Dim name As String = cookie.Value
     
            Dim age As String = Request.Cookies("Age").Value
     
            lblRetrieveCookies.Text = "You stored Name: " + name + ", and Age: " + age + ", as cookies"
        Else
                lblRetrieveCookies.Text = "No Cookies exist"
        End If
    End Sub
     
    Visual C#
     
    protected void ButtonRetrieveCookies_Click(object sender, EventArgs e) {
        if (Request.Cookies["Name"] != null && Request.Cookies["Age"] != null) {
            HttpCookie cookie = Request.Cookies["Name"];
            string name = cookie.Value;
     
            string age = Request.Cookies["Age"].Value;
     
            lblRetrieveCookies.Text = "You stored Name: " + name + ", and Age: " + age + ", as cookies";
        }
        else {
                lblRetrieveCookies.Text = "No Cookies exist";
        }
    }
     
  6. Press F5 and see the result in browser. Provide values in Text boxes and click on “Create Cookies” button. Now click on Retrieve Cookies button to see values.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to use jQuery Datepicker in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net