Working with Cookies
Cookies : The information which is stored in Client Machine.
Syntax for creating the cookie
To create or retrieve information from cookie we have a class called as
1) HttpCookie object=new HttpCookie(cookiename, cookievalue);
Now to store the above cookie we have to write the next line asResponse.Cookies.Add(object);
2) HttpCookie object=new HttpCookie(cookiename);object.Value=cookie.value;Response.Cookies.Add(object);
Retrieving the information from a Cookie
Syntax1) HttpCookie object=Request.Cookies["Cookiename"];variable=object.Value;
2) variable=Request.Cookies["Cookiename"].value;
Cookies are classified into Two Types
1 In Memory Cookies2 Out Memory Cookies
In Memory Cookies: Are again classified into two types1 Single Value Cookie2 Multi Value Cookie
Out Memory Cookies:
Are again classified into two types1 Single Value Cookie2 Multi Value Cookie
InMemory Cookie:The cookies which are stored in the browser process memory are called as inmemory cookie.
These cookies will automatically destroy themselves when the browser is closed.so the default life span of the inmemory cookie is till the browser is on.
SingleValueCookie
Which stores only the single cookie is called as single value cookie.
Example
Default.aspx
Default.aspx.cs
Multi Value Cookie
A cookie which holds multiple values in a single name.
How to Create MultiValue Cookie
HttpCookie object=new HttpCookie(cookiename);
object.Values["key"]="value";
or
object.Values.Add("key","value1");
object.Values.Add("key","value2");
To Store the Cookie
Response.Cookies.Add(object);
To Retrieve information
HttpCookie object =Request.Cookies["CookieName"];
To get Count
Object.Values.Count
To get Keys
Object.Values.GetKey(index);
To get KeyValues
Object.Values[index];
Object.Values.Get(index);
Example
Default.aspx
Default.aspx.cs
Output
When Click on Create Cookie
It displays a Message as Cookie Created.
When Click on Display Cookie
Out Memory Cookie
A cookie which is stored permanently on the hard disc of a client machine .
Syntax for Creating Single Value Cookie
HttpCookie obj=new HttpCookie("cookiename");
obj.Value=someValue;
obj.Expires=DateTime.Now.AddMinutes();
Supported Methods
AddSeconds();
AddHours();
AddMonths();
AddYears();
AddDays();
DateTime.MaxValue
To Store Cookie in the Hard Disc
Response.Cookies.Add(object);
Default.aspx
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="UserName"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:CheckBox ID="CheckBox1" runat="server" Text="Remember UserName & Password"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</td>
</tr>
</table>
Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["username"] != null && Request.Cookies["password"] != null)
{
if (Request.Cookies["username"].Value.ToString() == "uday" && Request.Cookies["password"].Value.ToString() == "uday")
{
Response.Redirect("Default2.aspx");
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (txtUserName.Text == "uday" && txtPassword.Text == "uday")
{
if (CheckBox1.Checked == true)
{
HttpCookie cookie = new HttpCookie("username", txtUserName.Text);
cookie.Expires = DateTime.MaxValue;
HttpCookie p = new HttpCookie("password", txtPassword.Text);
p.Expires = DateTime.MaxValue;
Response.Cookies.Add(cookie);
Response.Cookies.Add(p);
}
}
}
}
Output
________________________________________________________________
________________________________________________________________
Example for writing the cookies
Response.Cookies["uday"].Value = "abc";
Response.Cookies["uday"].Expires = DateTime.Now.AddDays(1);
_________________________________________________________________________________
HttpCookie anvesh= new HttpCookie("goud");
anvesh.Value = DateTime.Now.ToString();
anvesh.Expires = DateTime.Now.AddDays(1);Response.Cookies.Add(anvesh);
__________________________________________________________
Reading Cookies
if(Request.Cookies["uday"] != null)
lblCookie.Text = Server.HtmlEncode(Request.Cookies["uday"].Value);
__________________________________________________________
Deleting Cookies
HttpCookie demoCookie = new HttpCookie("uday");demoCookie.Value = DateTime.Now.ToString();demoCookie.Expires = DateTime.Now.AddDays(-1);Response.Cookies.Add(uday);
________________________________________________________________
Advantages
1 Cookies are stored on client.
2 Easy to Implement
3. You can configure cookies to expire when the browser session ends (session cookies) or they can exist for a specified length of time on the client computer (persistent cookies).
Disadvantages
1 User can delete cookie
2 No Security