How to maintain Cookie state in Application


In this article I'm trying to explain What is Cookie and how we use Cookies in Our Application and how to maintain state using Cookie and how to call the cookie in page wise and how to assign cookie value to store in a page.

How to maintain Cookie state in Application:



In this article I'm trying to explain What is Cookie and how we use Cookies in Our Application and how to maintain state using Cookie.

Cookies:



If we want to maintain state for multiple pages of the website we have to use other concepts provided by ASP.net and also by HTTP.
HTTP provides a concept called COOKIES which are supported by all web development coding; using COOKIES we maintain state for small amount of information.

Limitations Of cookies:



1) Size limitation ( 4 kb)
2) Type of data limitations ( string)
3) No.of cookies ( 20 cookies / domain)
4) Not secured
a) Client side - dependent on Browser
b) Travel in plain Textual format. (HTTP Headers)
c) Remote site – Access client Resources.

How to make COOKIES secured:



By default cookies are not secured to make it secure we have to follow 2 things.

1) Encrypt values using some encryption algorithms supported by .net (or) which are provided by third party for .net( RS Algorithem), and store them as part of cookie.

2) Use secure property of cookie and communicate using HTTPS. For this server should be enabled (or) installed with SSL certificates. HTTPS communication mode means entire page data will be encrypted and used in Request / Response.

How to work with Cookie in ASP.net:



System.web.HTTPCookie is the class using which we can create cookie in ASP.net.

1) Create a new project and give a name for that and then right clicking of that then create a page using NewItem and give a name for that as “Home.aspx”.

2) Drag & drop 1 label and 1 linkbutton controls in that.

3) Wrote below lines of code under page load event of a home page


protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["ctry"] != null)
Label1.Text = Request.Cookies["ctry"].Value;
else
Label1.Text = "INDIA";

}


4) and then wrote below lines of code under linkbutton click event.

protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("Change.aspx");
}


5) after that prepare a new page and give a name for that as Change.aspx and design a page just simply drag & drop 1 listbox control and one button control.

6) Add items to listbox control as India, Australia, South Africa , England, USA, UK..

7) And wrote below lines of code under button click event of change.aspx.

protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie obj = new HttpCookie("ctry");
obj.Value = ListBox1.SelectedValue;
Response.Cookies.Add(obj);
Response.Redirect("Home.aspx");
}


8) In the above example we create a cookie which is stored in memory also called as in- memory cookie. If we want to preserve user values then we can create cookie as persistent by providing expiry date and time. Expires is the property using which we can make a cookie as persistent cookie.

protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie obj = new HttpCookie("ctry");
obj.Value = ListBox1.SelectedValue;
obj.Expires=DateTime.Now.AddDays(2); // Persistent cookie
Response.Cookies.Add(obj);
Response.Redirect("Home.aspx");
}


Output:



1

2

3

Conclusion:



Using Cookies we can maintain the state in multiple pages with secured. But the drawback is we can't maintain the status of large amount of data and there is a size limitation to maintain the status.


Attachments

  • Source Code (45188-2291-Source-Code.zip)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: