ASP.NET 2.0 Profile: Simple User Personalization for Your Web Apps

Most enterprise Web applications need to track user preferences across multiple visits. In ASP.NET 1.x, you need to write lots of code to create this functionality. ASP.NET 2.0 introduces a new object, named profile, that simplifies the steps involved in tracking personalization information. This article provides an introduction to the profile object and differentiates it from the session object. It also demonstrates how to define user profiles, both simple name/value pair profiles and profile groups. Finally, it explains the procedures involved in configuring profiles to work with different providers.

An Overview of Profile Object

In an ASP.NET profile, information is stored in a persistent format and is associated with an individual user. The ASP.NET profile allows you to easily manage user information without having to create and maintain your own database. You can store any type of object in the profile because of its generic storage system. In addition, you also can make the same data available in a type-safe manner.

Is the Profile Object the Same as the Session Object?

At first look, the profile object might look very similar to the session object. But, there are a number of differences between the two objects. Table 1 outlines these differences.

Table 1. Differences Between the Profile and Session Objects

Characteristics Profile Session
Scope Each user has his own profile object Each user has his own session object
Strongly typed nature Profile object is strongly typed Session object is not strongly typed and requires type casting when assigning and retrieving from the session object
Persistent duration Profile values are available for the users, even between visits Session object contents are available only for the duration of the current browser session
Persistent location Profile object can be stored in a SQL Server Express database or in a SQL Server database and can be configured through the Web Site Administration tool Session object can be configured to be stored in a database, IIS in-process, or in a session state server, depending on the configuration setting
Performance Profile may have a negative impact on performance because of the chatty interface between the profile object and the persistent data store Can be configured using properties such as EnableSessionState attributes at the page level
IntelliSense Provides IntelliSense because of its strongly typed nature No support for IntelliSense

Now that you have a general understanding of the important characteristics of the profile object, take a look at an example.

Defining User Profiles

You define a user profile within the application root web.config file. You cannot create a web.config file containing a profile section in an application subfolder. This means that you can have only one profile element declaration for one Web application. The following declaration in the web.config contains a simple profile definition:

           <add name="Name" type="System.String"/>
           <add name="UserCount" type="System.Int16"/>

As you can see, the profile defines two properties: Name and UserCount. The default data type for profile properties is the System.String data type. In the above example, the Name property explicitly declares the data type to be of System.String. The UserCount property is assigned the type Int16 because it is used to represent an integer value.

Once you define a profile, whenever someone requests a page from the Web site ASP.NET automatically generates a class named ProfileCommon that corresponds to the profile definition. This class is stored in the Temporary ASP.NET Files directory, and an instance of this class is made available through the profile property of the HttpContext object.

The following section shows how to utilize this class to access the profile properties.

Working with User Profiles from an ASP.NET Page

Once you have the profiles declared, the next step is to access the profile object from within the ASP.NET page so that you can set or get the values stored in the profile properties. The following code uses the Name and UserCount properties declared in the previous section:

<%@ Page Language="C#" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
    Profile.UserCount += 1;
void btnGetProfileValues_Click(object sender, EventArgs e)
    lblName.Text = Profile.Name;
    lblUserCount.Text = Profile.UserCount.ToString();
void btnSetName_Click(object sender, EventArgs e)
    Profile.Name = txtName.Text;
<html  >
<head runat="server">
  <title>Accessing Profile object from an ASP.NET Page</title>
<form id="form1" runat="server">
    Name : <asp:TextBox runat="server" ID="txtName" />
    <asp:Button runat="server" ID="btnSetName"
      Text="Set Name" OnClick="btnSetName_Click" /><br />
    <asp:Panel ID="Panel1" runat="server"
      Height="224px" Width="265px" BorderColor="LightGray"
      <asp:Button runat="server" ID="btnGetProfileValues"
        Text="Get Profile Values"
        OnClick="btnGetProfileValues_Click" />
      <br/> <br /><br/>
      Profile Values: <br />
      Name: <asp:Label runat="server" ID="lblName"/>
      UserCount: <asp:Label runat="server" ID="lblUserCount"/>

The above listing declares two command buttons. The first, btnSetName, assigns the name entered in the textbox to the Profile.Name property. The click event of the second command button, btnGetProfileValues, results in the values of the profile properties being displayed in the label controls that in turn are contained in a Panel control. In the Page_Load event, the UserCount property is incremented by 1. As you can see from the code listing, setting the value of a profile property is very simple:

Profile.Name = txtName.Text;

Similarly, retrieving the values stored in the profile object is also very simple and straightforward:

lblName.Text = Profile.Name;

Note that you don’t need to typecast when retrieving the profile values because the properties stored in the profile object are strongly typed. Navigate to the above page using your browser, set the name, and click on the Get Profile Values button. You should see an output similar to Figure 1.

Figure 1. Output from “Get Profile Values”

This example enabled Windows authentication through IIS and enabled impersonation through the following settings in the web.config file:

<identity impersonate="true"/>

Note that it enabled impersonation so that the ASP.NET code has sufficient permissions to create a new SQL Server Express database for the first time.

How Profile Properties Are Stored

You might be wondering where exactly the profile properties Name and UserCount are stored in the above example. By default, ASP.NET 2.0 comes with two profile providers: SQL Server Express and SQL Server. By default, the SQL Server Express provider is used. (A later section will show how to change the provider.)

If you refresh your project listing in Solution Explorer, you will notice a database file called ASPNETDB.MDF within the App_Data folder. If you double-click on this database, you will see that the database is opened through the Server Explorer view. Within this database, you will see a table called aspnet_Profile. Figure 2 shows the contents of the table and the values that have been saved through the Profile properties shown in the previous example.

Figure 2. Contents of Table and Values in Profile Properties

More by Author

Must Read