Monday, 25 July 2011

Reading and Writing Cookies in ASP.NET and C#


This tutorial will show how we can both write to and read from a simple cookie. C# version.

Using Cookies in web pages is very useful for temporarily storing small amounts of data, for the website to use. These Cookies are small text files that are stored on the user's computer, which the web site can read for information; a web site can also write new cookies.

An example of using cookies efficiently would be for a web site to tell if a user has already logged in. The login information can be stored in a cookie on the user's computer and read at any time by the web site to see if the user is currently logged in. This enables the web site to display information based upon the user's current status - logged in or logged out.

Cookies can be very powerful, yet are very small text files and, fortunately, ASP.NET makes it easy for us to both read and write cookies.

In this example, we will have two ASPX pages: One to write a cookie, and one to read this same cookie.
The first ASPX page will look something like this:

<form id="form1" runat="server">
Cookie Name <asp:textbox id="NameField" runat="server"/><br />
Cookie Value <asp:textbox id="ValueField" runat="server"/><br />
<asp:button ID="Button1" text="WriteCookie" onclick="WriteClicked" runat="server" /><br />
<asp:HyperLink ID="lnkRead" NavigateUrl="Read.aspx" runat="server" Visible="false">Read the cookies <br />
</form>

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

The code-behind for this page will look something like this:

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 Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
lnkRead.Visible = true;
lnkRead.NavigateUrl = "Read.aspx?cookie=" + NameField.Text.ToString();
}
}

protected void WriteClicked(Object Sender, EventArgs e)
{
//Create a new cookie, passing the name into the constructor
HttpCookie cookie = new HttpCookie(NameField.Text);

//Set the cookies value
cookie.Value = ValueField.Text;

//Set the cookie to expire in 1 minute
DateTime dtNow = DateTime.Now;
TimeSpan tsMinute = new TimeSpan(0, 0, 1, 0);
cookie.Expires = dtNow + tsMinute;

//Add the cookie
Response.Cookies.Add(cookie);

Response.Write("Cookie written. <br><hr>");
}
}

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

We also create a page to read the cookie, which will be as follows:

<form id="form1" runat="server">
<asp:Label ID="lblCookie" runat="server"></asp:Label>
</form>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

The code-behind for this page will look something like this:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Read : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["cookie"] != null)
{
ReadCookie();
}
}

protected void ReadCookie()
{
//Get the cookie name the user entered
String strCookieName = Request.QueryString["cookie"].ToString();

//Grab the cookie
HttpCookie cookie = Request.Cookies[strCookieName];

//Check to make sure the cookie exists
if (cookie == null)
{
lblCookie.Text = "Cookie not found. <br><hr>";
}
else
{
//Write the cookie value
String strCookieValue = cookie.Value.ToString();
lblCookie.Text = "The " + strCookieName + " cookie contains: <b>" + strCookieValue + "</b><br><hr>";
}
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment