Saturday, August 23, 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 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 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>

IN C#:
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;
            }
        }

IN VB:

Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
        Dim ck As New HttpCookie("book")
        ck("bookname") = txtname.Text
        Response.Cookies.Add(ck)
        lblerror.Text = "Cookies Created Successfully"
        txtname.Text = String.Empty
    End Sub

    Protected Sub btncheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btncheck.Click
        lblerror.Text = ""
        Dim ck As HttpCookie = Request.Cookies("book")
        Dim bookname As String
        If ck IsNot Nothing Then
            bookname = ck("bookname")
            txtname.Text = bookname
        End If
    End Sub

Create persist cookie:

IN C#:

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;
        }

In VB:

Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
        Dim ck As New HttpCookie("book")
        ck("bookname") = txtname.Text
        ck.Expires = DateTime.Now.AddDays(1)
        Response.Cookies.Add(ck)
        lblerror.Text = "Cookies Created Successfully"
        txtname.Text = String.Empty
    End Sub

Example to remove persist cookies before expiration time:

IN C#:
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;
        } 
  
IN VB:

Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
        Dim ck As 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

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

Is this article helpful for you?
If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment