Tuesday, 4 June 2013

Cookies Example2

Using Cookies in ASP.NET Web Application



Introduction

Cookies are one of state management technique. Sometimes when you open a website it stores a cookie in your computer so that if you revisit the website, it can identify you for some specific needs, for example when you login to any website using your username and password provided to you, Website can ask you to remember password so when you opt this option, it stores your identity to the cookies as a result you no longer need to login again and again. Website automatically reads cookie for your identity and you are logged in. Another example can be a shopping cart, Items can be stored in cookies so the user can come back and continue to his shopping cart.
Cookies are maintained by your web browser and are browser specific. Cookies are very simple to use and implement. Some disadvantages are there in cookies as data is stored in simple text format means its not secure, browser can be set to not accept cookies form web servers, maximum number of cookie storage and size of cookie is limited.

Using Cookies in ASP.NET

(You can download a sample project at the end of this article)
As now I assume that you have basic understanding about cookies or state management. We will take an example in which we store username and his occupation in cookies and then retrieve cookie values.
Create a New Asp.NET website and add a page default.aspx with the following code
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<head runat="server">
 <title></title>
 <style type="text/css">
 .style1
 {
 width: 101px;
 }
 </style>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 
 <table style="width:100%;">
 <tr>
 <td class="style1">
 Name</td>
 <td>
 <asp:TextBox ID="NameTextBox" runat="server"></asp:TextBox>
 </td>
 </tr>
 <tr>
 <td class="style1">
 Occupation</td>
 <td>
 <asp:TextBox ID="OccupationTextBox" runat="server"></asp:TextBox>
 </td>
 </tr>
 <tr>
 <td class="style1">
 &nbsp;</td>
 <td>
 <asp:Button ID="StoreButton" runat="server" onclick="StoreButton_Click"
 Text="Store In Cookie" />
 <asp:Button ID="RetrieveButton" runat="server" onclick="RetrieveButton_Click"
 Text="Retrive Cookies Values" />
 </td>
 </tr>
 </table>
 
 </div>
 <asp:Label ID="ResultLabel" runat="server"></asp:Label>
 </form>
</body>
</html>
Add following code into its code behind file Default.aspx.cs file
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page
{
 protected void StoreButton_Click(object sender, EventArgs e)
 {
 HttpCookie usercookie = new HttpCookie("userdetail");//create cookie object
 usercookie["name"] = NameTextBox.Text; //Storing information in cookie
 usercookie["occupation"] = OccupationTextBox.Text;
 usercookie.Expires = DateTime.Now.AddDays(1); // Cookie will be set to keep values only for 1 day.After it cookie will be destroyed automatically
 Response.Cookies.Add(usercookie); // Storing the cookie
 ResultLabel.Text = "Cookies are stored";
 }
 protected void RetrieveButton_Click(object sender, EventArgs e)
 {
 HttpCookie readcookie = Request.Cookies["userdetail"];
 ResultLabel.Text = string.Format("Name: {0}, Occupation: {1}", readcookie["name"], readcookie["occupation"]);
 }
}
In the code you can see HttpCookie class performs all the task related to the cookie from creating it to retrieval of its values. HttpCookie class is available in System.Web namespace that we have defined at the top through using statement.
We have also set the cookie Expiry time that specifies how long it can last in user’s system, after that it will be removed automatically.
You can download sample source code below.

No comments:

Post a Comment