oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Personalization in ASP.NET
Pages: 1, 2, 3

Anonymous Personalization

Anonymous personalization is useful for several reasons. One good scenario is the shopping cart example. A user might add items to the cart (via the Profile properties) and log in only when he is ready to check out.

ASP.NET supports anonymous personalization and assigns a Globally Unique Identifier (GUID) to identify an anonymous user. To do so, you need to add the <anonymousIdentification> element to your web.config file:

  <anonymousIdentification enabled="true" />

To illustrate anonymous personalization, I will add a new web form called products.aspx to the project (see Figure 12).

Figure 12. Adding a new form to the project
Figure 12. Adding a new form to the project

As the products.aspx page is in the root of the project, anonymous users can access the page. Figure 13 shows the content of the page. It contains two ImageButton controls.

Figure 13. Content of Products.aspx
Figure 13. The content of products.aspx

When the user clicks on the image, the product's ISBN will be added to the Profile properties (which I will add shortly):

Sub imgBook1_Click(ByVal sender As Object, ByVal e As _
  Profile.Cart += "0-596-00768-X;"
  Response.Write("'Just a Geek' added!")
End Sub

Sub imgBook2_Click(ByVal sender As Object, ByVal e As _
  Profile.Cart += "0-596-00733-7;"
  Response.Write("'We the media' added!")
End Sub

For simplicity, I will implement the shopping cart as simply a string containing a list of ISBNs separated by semicolons. In reality, you can map your profile property to a class that you define in your project (such as a shopping cart class).

The following shows the addition to the web.config file:

<anonymousIdentification enabled="true" />
    <add name="FirstName" type="System.String"/>
    <add name="LastName" type="System.String"/>
    <add name="Cart" allowAnonymous="true"

Do note that you need to add the allowAnonymous attribute to each property of the Profile object that you want to allow for anonymous access. In this example, only the Cart property is allowed anonymous access.

Load the products.aspx page (make sure you are not logged in) using Internet Explorer, and click once on each image. Now, examine the aspnet_Profile table again (see Figure 14). You should see a third row containing the shopping cart values. It is associated with a UserName that contains some random characters (this is the GUID used to identify the anonymous user).

Figure 14. Anonymous Personalization
Figure 14. Anonymous personalization

Migrating from Anonymous Personalization to Authenticated Personalization

In the last section, you saw how anonymous users can also use the Profile object to add items to their shopping carts. When the user is ready to check out and proceed to payment, he would need to log in. When he logs in (through login.aspx), all of the profile properties saved when he was an anonymous user would be lost. Therefore, you would need to manually migrate his anonymous profile to his authenticated profile.

Let's now add a new web form to our project and name it checkout.aspx (located within the Members folder). This page will list all of the items added to the shopping cart by the user.

Also, add a hyperlink to the products.aspx page that links to the checkout.aspx page.

When an anonymous user logs in, an event called Profile_MigrateAnonymous would be fired. This event should be serviced in a Global Application Class called global.asax. To add a Global Application Class to your project, right-click the project name and select Add New Item..., and then select Global Application Class.

To transfer the anonymous profile to an authenticated profile, do the following:

Sub Profile_MigrateAnonymous(ByVal sender As Object, _
               ByVal e As ProfileMigrateEventArgs)
  Dim anonymousProfile As ASP.HttpProfile = _
  If anonymousProfile.Cart IsNot Nothing Then
    Profile.Cart = anonymousProfile.Cart
  End If
End Sub

Figure 15 shows the new additions.

Figure 15.  The new files in the project
Figure 15. The new files in the project

In the checkout.aspx page, list out the content of the shopping cart to ensure that the cart content has been migrated successfully:

Sub Page_Load(ByVal sender As Object,
              ByVal e As System.EventArgs)

  Response.Write("Shopping cart content: " " Profile.Cart)
End Sub

You can now test out the process. Figure 16 shows the steps involved:

Figure 16. Testing the migration of a user’s profile
Figure 16. Testing the migration of a user's profile


Personalization is a very useful feature in ASP.NET 2.0. It is also the foundation for other new features in ASP.NET 2.0, such as web parts and themes. In the next article, I will talk more about applying skins and themes to your ASP.NET 2.0 web application.

Wei-Meng Lee (Microsoft MVP) is a technologist and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies.

Return to