Sunday 31 August 2014

Explain cookies in asp.net OR Explain cookies with example in asp.net

Introduction: In this article today I will explain cookies in asp.net OR Explain cookies with example in asp.net


Description:

Cookies are the small file of information stored on user’s machine. Cookies allow the web application to save information for user and use it when needed. It is saved either in memory of web browser or as a text file in the client’s file system.


Types of Cookies:
    1.      persist  cookies:
It is also known as permanent cookies. Persist cookies are store in hard disk until they expired or deleted. These are not affected by the browser setting.

    2.      Non persist cookies (temporary cookies): 
It is also known as session cookies. It stored in memory of web browser, as we close the browser it’s lost.

Advantages of cookies:
     1.      No server resources are required.
     2.      Cookies are light weight and easy to implement.

Disadvantages of cookies:
     1.      Cookies can be disabled on user’s browser.
     2.      Not secure because it stored data in simple text format.
     3.      Maximum browser support cookies up to 4096 bytes (4 kB) but vary for    browser to browser.
     4.      Maximum number of cookies allowed is also limited. A website can have maximum 20 cookies per website.
    5.      No security for case sensitive data.

Example:

Add a webform to project and design the page as mention below:


<fieldset style="width:400px;height:auto;">
            <legend>Cookies Example:</legend>
    <table>
        <tr><td>Name:</td><td><asp:TextBox ID="txtname" runat="server"></asp:TextBox></td></tr>
        <tr><td></td><td><asp:Label ID="lblerror" runat="server"></asp:Label></td></tr>
        <tr><td><asp:Button ID="btnsubmit" runat="server" Text="Submit"OnClick="btnsubmit_Click" /></td><td><asp:Button ID="btncheck"runat="server" Text="Read Cookies" OnClick="btncheck_Click" /></td></tr>
    </table>
            </fieldset>


Code Behind:
protected void btnsubmit_Click(object sender, EventArgs e)
        {
            HttpCookie ck = new HttpCookie("book");
            ck["bookname"] = txtname.Text;
            Response.Cookies.Add(ck);
            lblerror.Text = "Cookies Created Successfully";
            txtname.Text = string.Empty;
        }      
        protected void btncheck_Click(object sender, EventArgs e)
        {
            lblerror.Text = "";
            HttpCookie ck = Request.Cookies["book"];
            string bookname;
            if (ck != null)
            {
                bookname = ck["bookname"];
                txtname.Text = bookname;
            }
        }


Create persist cookie:


protected void btnsubmit_Click(object sender, EventArgs e)
        {
            HttpCookie ck = new HttpCookie("book");
            ck["bookname"] = txtname.Text;
            //cookie expire in 1 day
            ck.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(ck);
            lblerror.Text = "Cookies Created Successfully";
            txtname.Text = string.Empty;
        }


Example to remove persist cookies before expiration time:

protected void btnsubmit_Click(object sender, EventArgs e)
        {
            HttpCookie ck = new HttpCookie("book");
            ck["bookname"] = txtname.Text;
            //Expire the cookie before expiration time
            ck.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(ck);
            lblerror.Text = "Cookies Created Successfully";
            txtname.Text = string.Empty;
        } 

  

No comments:

Post a Comment