ASP.NET


Cookies tutorials
-
A cookie is a small piece of text stored on user's computer. Usually, information is stored as name-value pairs. Cookies are used by websites to keep track of visitors. Every time a user visits a website, cookies are retrieved from user machine and help identify the user.
-
Advantages of using cookies are:
-
Configurable expiration rules: The cookie can expire when the browser session ends, or it can exist indefinitely on the client computer, subject to the expiration rules on the client.
-
No server resources are required: The cookie is stored on the client and read by the server after a post.
-
Simplicity: The cookie is a lightweight, text-based structure with simple key-value pairs.
-
Data persistence: Although the durability of the cookie on a client computer is subject to cookie expiration processes on the client and user intervention, cookies are generally the most durable form of data persistence on the client.
-
-
Disadvantages of using cookies are:
-
Size limitations: Most browsers place a 4096-byte limit on the size of a cookie, although support for 8192-byte cookies is becoming more common in newer browser and client-device versions.
-
User-configured refusal: Some users disable their browser or client device's ability to receive cookies, thereby limiting this functionality.
-
Potential security risks: Cookies are subject to tampering. Users can manipulate cookies on their computer, which can potentially cause a security risk or cause the application that is dependent on the cookie to fail.
-
Example:
-
Here we have creating and retriving the cookies using HttpCookie class. We have used one textbox for cookie name, a button and alevel that will display the cookie name.
Cookies1Example.aspx.cs Code:
|
Following example shows how to create a cookies and read the cookies in ASP.NET |
![]() |
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class Cookies1Example : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){HttpCookie UserCookies = new HttpCookie("UserCookies");UserCookies.Value = txtusername.Text;UserCookies.Expires = DateTime.Now.AddHours(1);Response.Cookies.Add(UserCookies);}protected void btncreatecookies_Click(object sender, EventArgs e){string username = Request.Cookies["UserCookies"].Value;lblcookiesname.Text = username;}} |
|
|
Cookies1Example.aspx Code:
|
Following example shows how to create a cookies and read the cookies in ASP.NET |
![]() |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cookies1Example.aspx.cs" Inherits="Cookies1Example" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title>cbtSAM ASP.NET Cookies Example</title></head><body><form id="form1" runat="server"><div>UserName :<asp:TextBox ID="txtusername" runat="server"></asp:TextBox><br /><br /><asp:Button ID="btncreatecookies" runat="server"onclick="btncreatecookies_Click" Text="Create Cookies" /><br /><asp:Label ID="lblcookiesname" runat="server" Text=""></asp:Label></div></form></body></html> |
Output |
After running the project it will display such as.Here we have created a cookies UserName is watson,below output shows the created cookie name. |
Example 2: Here is an example where we save a users choice of background color:
CookiesExample.aspx.cs Code
|
Following example shows the use of ASP.NET Cookies: |
![]() |
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;public partial class CookiesExample : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){if (Request.Cookies["BackgroundColor"] != null){ColorSelector.SelectedValue = Request.Cookies["BackgroundColor"].Value;bdytag.Style["background-color"] = ColorSelector.SelectedValue;}}protected void ColorSelector_SelectedIndexChanged(object sender, EventArgs e){bdytag.Style["background-color"] = ColorSelector.SelectedValue;HttpCookie cookie = new HttpCookie("BackgroundColor");cookie.Value = ColorSelector.SelectedValue;cookie.Expires = DateTime.Now.AddHours(1);Response.SetCookie(cookie);}} |
|
|
-
The page simply contains a DropDownList control, which automatically posts back each time a new item is selected. It has 3 different colors, besides the default one, which is simply white. Once a new item is selected, the ColorSelector_IndexChanged method is fired, from our CodeBehind file:
CookiesExample.aspx Code
|
Following example shows the use of ASP.NET Cookies: |
![]() |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CookiesExample.aspx.cs" Inherits="CookiesExample" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title>cbtSAM ASP.NET Cookies Example</title></head><body runat="server" id="bdytag"><form id="form1" runat="server"><div><asp:DropDownList runat="server" id="ColorSelector" autopostback="true"onselectedindexchanged="ColorSelector_SelectedIndexChanged" ><asp:ListItem value="White" selected="True">Select color..</asp:ListItem><asp:ListItem value="Red">Red</asp:ListItem><asp:ListItem value="Green">Green</asp:ListItem><asp:ListItem value="Blue">Blue</asp:ListItem></asp:DropDownList></div></form></body></html> |
Output |
|