Monday, 25 July 2011

Reading and Writing Cookies in ASP.NET and VB


This tutorial will show how we can both write to and read from a simple cookie. VB 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.

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

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>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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

Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub

Protected Sub WriteClicked(ByVal Sender As Object, ByVal e As EventArgs) Handles Button1.Click
lnkRead.Visible = True
lnkRead.NavigateUrl = "Read.aspx?cookie=" & NameField.Text.ToString()

'Create a new cookie, passing the name into the constructor
Dim cookie As New HttpCookie(NameField.Text)

'Set the cookies value
cookie.Value = ValueField.Text

'Set the cookie to expire in 1 minute
Dim dtNow As DateTime = DateTime.Now
Dim tsMinute As New TimeSpan(0, 0, 1, 0)
cookie.Expires = dtNow.Add(tsMinute)

'Add the cookie
Response.Cookies.Add(cookie)

lblRead.Text = "Cookie written."
End Sub
End Class

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>

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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

Partial Class Read
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString("cookie") IsNot Nothing Then
ReadCookie()
End If
End Sub

Protected Sub ReadCookie() 'Get the cookie name the user entered
Dim strCookieName As String = Request.QueryString("cookie").ToString()

'Grab the cookie
Dim cookie As HttpCookie = Request.Cookies(strCookieName)

'Check to make sure the cookie exists
If cookie Is Nothing Then
lblCookie.Text = "Cookie not found. <br><hr>"
Else 'Write the cookie value
Dim strCookieValue As String = cookie.Value.ToString()
lblCookie.Text = "The " & strCookieName & " cookie contains: <b>" & strCookieValue & "</b><br><hr>"
End If
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment