Sunday, November 20, 2016

State Management in asp.net- Part 3


Cookies:


It is a client side state management technique. Cookies are created at client hard drive by browser. When a user visit the website first time, server gets the request and send the cookies information to browser and browser write the information into hard drive of client machine. When user visits website again the cookies information is read by the browser from client machine if exists and sent to the server.

Cookies are two types:

  1.   Persistent: If we store information in cookie with an expiration date then it is called persistent cookie. Means the information stored in cookie will expire after that expiration time.

  2.  Non-Persistent: We store information in cookie without expiration date. Means the information stored in cookie will be stored till user is using the same browser. Use non persistent cookie for the information that is secure.

Let us understand it with an example:

Cookies_Demo2.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cookies_Demo2.aspx.cs" Inherits="Cookies_Demo2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        Password:<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
        <asp:Label ID="lblResult" runat="server"></asp:Label>

        <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit"/>
    </div>
    </form>
</body>
</html>

Cookies_Demo2.aspx.cs:

using System;

public partial class Cookies_Demo2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["name"] != null)
        {
            txtName.Text = Request.Cookies["name"].Value.ToString();
        }
        if (Request.Cookies["password"] != null)
        {
            txtPassword.Text = Request.Cookies["password"].Value.ToString();
        }
       
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Response.Cookies["name"].Value = txtName.Text;
        Response.Cookies["password"].Value = txtPassword.Text;
        Response.Cookies["name"].Expires = DateTime.Now.AddMinutes(10);
        Response.Cookies["password"].Expires = DateTime.Now.AddMinutes(10);
        lblResult.Text = "Request send successfully.";
    }
}

Here in the above example when user will click on “Submit” button after entering the user name and password server will create cookies “name” and “password” using the “Response” object.
If you want to make cookie as persistent the use the “Expires” property and set the value as mentioned above.

Now, close the browser and open it with same URL. If you open it within 10 minutes then information stored in cookie will assigned into the “txtName” and “txtPassword”. But after 10 minutes it will destroy.

Important Notes:

   1.   Use cookies for small size of information. Cookie size cannot be more than 4KB i.e. 4096 bytes
   2.   20 cookies are allowed per domain.