oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Personalization in ASP.NET

by Wei-Meng Lee 08/16/2004

Personalizing your web site is one of the tasks that you can do to enhance the experiences of users visiting your site. Personalization allows information about visitors to be persisted so that the information can be useful to the visitor when he visits your site again. For example, uses personalized recommendation to remember the books that I have purchased and make suitable recommendations based on my interests (see Figure 1).

Figure 1.’s personalized recommendation
Figure 1.'s personalized recommendation

In ASP.NET 2.0, you can now use personalization providers to personalize your site. This article will discuss the personalization feature in ASP.NET 2.0 and how you can use it to personalize your web application.

Using the Profile Object

In ASP.NET 1.x, the conventional method for storing personalized information is through the Session object. However, using the Session object has its disadvantages, such as its volatility (it's persisted in memory by default, unless it's saved on disk), late-binding (all session objects are stored as objects), large memory requirements, and inefficient loading mechanism.

You may also store users' information in databases, but that requires you to write custom code to save and retrieve users' information.

In ASP.NET 2.0, there is a new Profile object that allows users' data to be persisted in a much more efficient manner.

To see how to use the Profile object, I will show you how to build a simple web application that uses the Profile object to save users' information. To do so, create a new web site using Visual Studio 2005 (Beta 1 at this moment).

In the default page, populate the page with the following controls -- all of the controls are located in a Panel control (see Figure 2).

Figure 2. Populating the default page with all of the controls
Figure 2. Populating the default page with all of the controls

This page will prompt the user to enter his name when he first visits the page. The next time the user visits the page, the application will be able to remember his name. I will use the Profile object to save the user's name. To use the Profile object, you first need to add a web configuration file to your project. (In Solution Explorer, right-click on the project name and select Add New Item. Select Web Configuration File.) Add the <profile> element into the web.config file:

     <add name="FirstName" type="System.String"/>
     <add name="LastName" type="System.String"/>

Here, I have defined two Profile properties -- FirstName and LastName, which will be used to store a user's first and last name, respectively. To use the two Profile properties, simply prefix the property name with the Profile keyword. That is, use Profile.FirstName and Profile.LastName.

In the Page_Load event, I first check to see if the FirstName property is empty. If it is empty, I assume that the user has never visited the page before, and show the Panel control to allow the user to enter his first and last name. If it is non-empty, then I will retrieve the first and last name of the user from the Profile properties.

Sub Page_Load(ByVal sender As Object,
              ByVal e As System.EventArgs) _
  Handles Me.Load
  If Profile.FirstName <> "" Then
    Panel1.Visible = False
    Response.Write("Welcome back, " & Profile.FirstName & ", " & _
    Panel1.Visible = True
  End If
End Sub

With the Save button, you save the first and last names by simply assigning them to the Profile properties:

Sub btnSave_Click(ByVal sender As Object,
                  ByVal e As System.EventArgs)
  ' save the profile 
  Profile.FirstName = txtFirstName.Text
  Profile.LastName = txtLastName.Text
End Sub

Figure 3 shows what happens when the user visits the page for the first time.

Figure 3. User visiting the page for the first time
Figure 3. A user visiting the page for the first time

Figure 4 shows what happens when the same user visits the page again:

Figure 4. The same user visiting the page again
Figure 4. The same user visiting the page again

Related Reading

Programming ASP.NET
By Jesse Liberty, Dan Hurwitz

As you can see, the user's first and last names have been saved in the Profile properties. So where are these values stored?

Note: ASP.NET 2.0 comes with two profile providers -- Access and SQL Server. By default, the Access provider is used. To change to the SQL Server provider (or your own provider), use the ASP.NET Web Application Administration tool. Under the Provider tab, click "Select a different provider for each feature (advanced)" and then select the relevant provider in the Profile Provider box.

If you refresh your project listing in Solution Explorer, you will notice that within the Data folder there is a database file called ASPNetDB.mdb (see Figure 5).

Figure 5. The ASPNetDB.mdb file in the Data folder
Figure 5. The ASPNetDB.mdb file in the Data folder

Within this database, you will see a table called aspnet_Profile. Figure 6 shows the content of the table and the values that have been saved via the Profile properties. Also note the user name associated with the row. By default, ASP.NET uses Windows authentication, and so when I access my application, ASP.NET associates me with my Windows user name.

Figure 6. Examining the aspnet_Profile table
Figure 6. Examining the aspnet_Profile table

It is interesting to note how ASP.NET stores the Profile properties. In my case, I have two properties -- FirstName and LastName. The following table shows the value of each field in the table.

Field NameValues
PropertyNames FirstName:S:0:8:LastName:S:8:3:
PropertyValuesStringWei MengLee

It is not difficult to interpret the relationship between the PropertyNames and PropertyValuesString fields.

Pages: 1, 2, 3

Next Pagearrow