Saturday, October 2, 2010

Working with Cookies


Websites typically use session cookies to ensure that users are recognized when they move from page to page within one site and that any information you have entered is remembered. For example, if an e-commerce site which uses session cookies then items placed in a shopping cart would appear by the time you reach the checkout.
The browser is responsible for managing cookies on a user system. Cookies are sent to the browser via the HttpResponse object that exposes a collection called Cookies. You can access the HttpResponse object as the Response property of your Page class. Any cookies that you want to send to the browser must be added to this collection. When creating a cookie, you specify a Name and Value.
When a browser makes a request to the server, it sends the cookies for that server along with the request. In this ASP.NET application, we can read the cookies using the HttpRequest object, which is available as the Request property of Page class.
We added one text box, two buttons and 1 label to the web page. The text box is used for inputting the information of cookie. By clicking Add button, the sample application will create a new cookie. By clicking View button, you will see the cookie created.

protected void Button1_Click(object sender, EventArgs e)
{
Response.Cookies["MyCookie"]["Data"] = TextBox1.Text;
Response.Cookies["MyCookie"]["Time"] = DateTime.Now.ToString("G");
Response.Cookies["MyCookie"].Expires=DateTime.Now.AddMonths(1);
Label1.Text = "Cookie created!
" + "Your cookie contains:" + Request.Cookies["MyCookie"]["Data"] + "
" + Request.Cookies["MyCookie"]["Time"] + "
";
Response.Cookie("MyCookie").Expires=DateTime.FromString("2006-10-1");
}
protected void Button2_Click(object sender, EventArgs e)
{
if (Request.Cookies["MyCookie"] == null)
Label1.Text = "There is no cookie:";
else
Label1.Text = "Your cookie contains:" + "" + Request.Cookies["MyCookie"]["Data"] + "
" + Request.Cookies["MyCookie"]["Time"] + "
";
}

The front end Default.aspx page looks something like this:

asp:TextBox ID="TextBox1" runat="server"
asp:Button ID="Button1" runat="server" Text="Add" Width="70px" 
asp:Button ID="Button2" runat="server" Text="View" Width="84px"
asp:Label ID="Label1" runat="server" Text="" Width="138px"


The flow for the code behind page is as follows

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{

protected void Button1_Click(object sender, EventArgs e)
{
Response.Cookies["MyCookie"]["Data"] = TextBox1.Text;
Response.Cookies["MyCookie"]["Time"] = DateTime.Now.ToString("G");
Response.Cookies["MyCookie"].Expires=DateTime.Now.AddMonths(1);
Label1.Text = "Cookie created!
" + "Your cookie contains:" + Request.Cookies["MyCookie"]["Data"] + "
" + Request.Cookies["MyCookie"]["Time"] + "
";
Response.Cookie("MyCookie").Expires=DateTime.FromString("2006-10-1");
}
protected void Button2_Click(object sender, EventArgs e)
{
if (Request.Cookies["MyCookie"] == null)
Label1.Text = "There is no cookie:";
else
Label1.Text = "Your cookie contains:" + "" + Request.Cookies["MyCookie"]["Data"] + "
" + Request.Cookies["MyCookie"]["Time"] + "
";
}
}

No comments:

Post a Comment