Friday 10 October 2014

What are the Advantages and Disadvantages of Cookies

What is cookie?
----------------
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.
Purpose of cookie:

 1)Used to store user-specific information with in user system to reduce burden on server.

ie, user-specific inforamtion can be credentials(unam and pwd), sessionid,security token.


Types of cookies:

1)Inmemory cookie or Temporary cookie or Session cookie
2)Persistant cookie or permanent cookie
3)Dictionary cookie or Multivalued cookie(ie, one cookie multiple values)

1)Inmemory cookie or Temporary cookie:
Cookie can be maintained by the browser process memory is called "Inmemory cookie".

Eg:1)
Default.aspx
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("Uname", "ramesh");
Response.Cookies.Add(cookie);
Response.Redirect("Default2.aspx");
}

Default2.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["Uname"] != null)
Response.Write(Request.Cookies["Uname"].Value);
else Response.Write("Cookie not available");
}

2)Persistant cookie or permanent cookie
Cookies can be maintained with in hard disk memory of the client system

Eg:1)

protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["Uname"] != null)
{
//Response.Redirect("Default2.aspx");
TextBox1.Text = Convert.ToString(Request.Cookies["Uname"].Value);
TextBox2.Text = Convert.ToString(Request.Cookies["Pwd"].Value);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Cookies["Uname"] == null)
{
HttpCookie obj = new HttpCookie("Uname", txtUname.Text);
HttpCookie obj2 = new HttpCookie("Pwd", txtPwd.Text);
obj.Expires = DateTime.Now.AddDays(1);
obj2.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(obj);
Response.Cookies.Add(obj2);
Response.Redirect("Default2.aspx");
}
}

3)Dictionary cookie or Multivalued cookie
It used to store multiple name-value pairs in a single cookie.

Eg:1)
Default.aspx
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie objCookie = new HttpCookie("Trainees");
objCookie.Values.Add("Name1", "100");
objCookie.Values.Add("Name2", "200");
objCookie.Values.Add("Name3", "300");
objCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(objCookie);
Response.Redirect("Default2.aspx");
}

Default2.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["Trainees"] != null)
Response.Write(Request.Cookies["Trainees"]["Name2"]);
}


1)How do you create a Cookie that never expires?

Ans :- To create a Cookie that never expires, set the Expires property of the Cookie object to DateTime.MaxValue.

myCookie.Expires = DateTime.MaxValue;



Advantages 
1. Cookies do not require any server resources since they are stored on the client.
2. Cookies are easy to implement.

Disadvantages: 
1)Cookies can be disabled on user browsers
2)Cookies are transmitted for each HTTP request/response causing overhead on bandwidth
3)No security for sensitive data

Cookie Limitations:
1)Most browsers support cookies of up to 4096 bytes(4kbytes)
2)Most browsers allow only 20 cookies per site; if you try to store more, the oldest cookies are discarded.
3)Browser supports 300 cookies towards different websites.
4)Complex type of data not allowed(eg: dataset), allows only plain text (ie, cookie allows only string content)
5)Cookies are browser specific (ie, one browser type[IE] stored cookies will not be used by another browser type[firefox]).

No comments:

Post a Comment