Cookies in ASP.Net


Cookies are a State Management Technique that can store the values of control after a post-back. Cookies can store user-specific Information on the Client's machine like when the user last visited your site.

Basically Cookies are one of the following 2 types:
  1. Persistent Cookies: Persistent Cookies are Permanent Cookies stored as a text file in the hard disk of the computer.
  2. Non-Persistent Cookies: Non-Persistent cookies are temporary. They are also called in-memory cookies and session-based cookies. These cookies are active as long as the browser remains active, in other words if the browser is closed then the cookies automatically expire.
 
List of properties containing the HttpCookies Class:
  1. Domain:    Using these properties we can set the domain of the cookie.
  2. Expires:    This property sets the Expiration time of the cookies.
  3. Has Keys:  If the cookies have a sub key then it returns True.
  4. Name:      Contains the name of the Key.
  5. Path:         Contains the Virtual Path to be submitted with the Cookies.
  6. Secured:   If the cookies are to be passed in a secure connection then it only returns True.
  7. Value:       Contains the value of the cookies.

Limitation of the Cookies
  1. The size of cookies is limited to 4096 bytes.
  2. A total of 20 cookies can be used in a single website.
 Example:-

Cookie_webf1.aspx:-

<div align="center">
        <table>
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            :<asp:TextBox ID="txtName" runat="server">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <b>Email</b>
        </td>
        <td>
            :<asp:TextBox ID="txtEmail" runat="server">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <asp:Button ID="btnSendData" runat="server"
            Text="Redirect" onclick="btnSendData_Click" />
        </td>
    </tr>
</table>
    </div>


Cookie_webf1.aspx.cs(Persistent Cookies):-

 protected void btnSendData_Click(object sender, EventArgs e)
    {
          // Create the cookie object
    HttpCookie cookie = new HttpCookie("UserDetails");
    cookie["Name"] = txtName.Text;
    cookie["Email"] = txtEmail.Text;
    // Cookie will be persisted for 30 days
    cookie.Expires = DateTime.Now.AddDays(30);
    // Add the cookie to the client machine
    Response.Cookies.Add(cookie);

    Response.Redirect("~/cookie_webf2.aspx");
    }


Cookie_webf1.aspx.cs(Non Persistent Cookies):-

 protected void btnSendData_Click(object sender, EventArgs e)
    {
          // Create the cookie object
    HttpCookie cookie = new HttpCookie("UserDetails");
    cookie["Name"] = txtName.Text;
    cookie["Email"] = txtEmail.Text;
    // Add the cookie to the client machine
    Response.Cookies.Add(cookie);

    Response.Redirect("~/cookie_webf2.aspx");
    }


Cookie_webf2.aspx :-

 <div align="center">
    <table>
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            :<asp:Label ID="lblName" runat="server">
            </asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>Email</b>
        </td>
        <td>
            :<asp:Label ID="lblEmail" runat="server">
            </asp:Label>
        </td>
    </tr>
</table>
    </div>


Cookie_webf2.aspx.cs :-

  protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["UserDetails"];
        if (cookie != null)
        {
            lblName.Text = cookie["Name"];
            lblEmail.Text = cookie["Email"];
        }
    }

No comments:

Post a Comment

Xamarin Android Project App

Xamarin Android Project App 1. Xamarin -- Make a CALCULATOR Android App   https://drive.google.com/open?id=0B8OPCXLrtKPmWC1FWWtFM2lraVk...