A Deeper Look at Personalization using Visual Basic 2005 - Migrating the Anonymous Data to the Actual User’s Record (Page 3 of 4 )
When the user does log in, you must migrate the Profile data you’ve accumulated for the anonymous user to the authenticated user’s record (so that, for example, shopping cart items are not lost). You do this by writing a global handler in global.asax.
If your project does not yet have a global.asax file, right-click on the project and choose Add New Item. One of your choices will be Global Application Class, and it will default to the name global.asax (click Add). Within that class, add a method to handle theMigrateAnonymousevent that is fired when a user logs in, as shown in Example 12-21.
Example 12-21. MigrateAnonymous event handler
Sub Profile_MigrateAnonymous( _
ByVal sender As Object, ByVal e As ProfileMigrateEventArgs)
Dim anonymousProfile As ProfileCommon = _
Profile.GetProfile(e.AnonymousId)
If anonymousProfile IsNot Nothing And _
anonymousProfile.CHOSENBOOKS IsNot Nothing Then
For Each s As String In anonymousProfile.CHOSENBOOKS
Profile.CHOSENBOOKS.Remove(s) ' remove duplicates
Profile.CHOSENBOOKS.Add(s)
Next
End If
End Sub
The first step in this method is to get a reference to the profile that matches the AnonymousID passed in as a property of ProfileMigrateEventArgs:
Dim anonymousProfile As ProfileCommon = _
Profile.GetProfile(e.AnonymousId)
If the reference is notNothing, then you know that there is a matching anonymous profile, and that you may choose whatever data you need from that profile. In this case, you copy over theCHOSENBOOKScollection.
The user’s profile is updated, and the books chosen as an anonymous user are now part of that user’s profile, as shown in Figure 12-43.

Figure 12-43. Profiles merged
Next: Themes and Skins >>
More Visual Basic.NET Articles
More By O'Reilly Media
|
This article is excerpted from the book Programming Visual Basic 2005, written by Jesse Liberty (O'Reilly, 2005; ISBN: 0596009496). Check it out today at your favorite bookstore. Buy this book now.
|
|