7.4. Transfer an Anonymous Profile to an Authenticated Profile
Note: When an anonymous user is authenticated, you need to manually migrate his profile properties.
Although ASP.NET 2.0 lets you maintain information for both authenticated and anonymous users, you need to take special steps to preserve data when you authenticate an anonymous user. For example, an anonymous user may already have items in her shopping cart when she logs in. In this situation, profile data that was saved while she was an anonymous user would normally be lost when the user switches from using a GUID to using a user ID for identification. To migrate the profile of the user, you need to transfer whatever information has been saved as an anonymous profile to the user profile.
7.4.1. How do I do that?
In this lab, you will build on the previous lab, Section 7.3, and learn how you can migrate an anonymous profile to an authenticated profile once a user has been authenticated.
Using the project created in the last lab (C:\ASPNET20\chap07-Profile), add a new Web Form to the Members folder. Name the Web Form Checkout.aspx.
Add a Global.asax file to the project (right-click the project name in Solution Explorer and then select Add New Item...; select Global Application Class).
The Solution Explorer should now look like Figure 7-15.
Figure 7-16. Adding the Global.asax file to the project
Add the following method to Global.asax. The MigrateAnonymous event is raised whenever a user changes his status from anonymous to authenticated.
You can get the anonymous ID from the AnonymousId property and then use the GetProfile( ) method to retrieve the anonymous profile. The retrieved profile can then be assigned to the authenticated user profile.
Sub Profile_MigrateAnonymous(ByVal sender As Object, _ ByVal e As ProfileMigrateEventArgs) Dim anonymousProfile As ProfileCommon = _ Profile.GetProfile(e.AnonymousId) If anonymousProfile.shoppingcart IsNot Nothing Then Profile.shoppingcart = anonymousProfile.shoppingcart End If '---delete the items associated with the anonymous user ProfileManager.DeleteProfile(e.AnonymousID) '---clear the anonymous identifier from the request ' so that this event will not fire for an authenticated user AnonymousIdentificationModule.ClearAnonymousIdentifier( ) End Sub
In the code-behind of Checkout.aspx, code the following in the Form_Load event. The Checkout.aspx page will print out all the items in the cart:
Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles Me.Load Dim myCart As OReilly.Cart myCart = Profile.shoppingcart Dim item As OReilly.itemType Response.Write("Your cart contains:<br/>") For Each item In myCart.items Response.Write(item.isbn & " - " & item.qty & "<br/>") Next End Sub
Add a Button control to Products.aspx and set its Text property to Check Out (see Figure 7-16) so that the user can proceed to Checkout.aspx after he is done adding the item. Set the PostBackUrl property of the Button control to Members/Checkout.aspx.
Figure 7-17. Adding the Check Out button
To test the application, you should first load the Products.aspx page anonymously and then add a few items to your shopping cart. Once this is done, click the Check Out button to proceed to the checkout page. You will be prompted to log in. When you are authenticated, the MigrateAnonymous event fires and the profile properties are transferred. The Checkout.aspx page will list all the items in the shopping cart.
Tip: Check the aspnet_profile table to verify that the profiles have been migrated successfully.
7.4.2. What about...
...the old entries in the aspnet_Profile table that were saved by the anonymous user? Will they be deleted when the user's profile is moved?
The answer is no. You need to manually clear away the entries. Fortunately, ASP.NET 2.0 comes with the ProfileManager class (which contains the DeleteProfile( ) method) to help you manage profile properties.
7.4.3. Where can I learn more?
Check out the Visual Studio 2005 documentation to see how you can use the ProfileManager class to manage profiles and generate reports on profiles. Also, check out the following article for more information on the Profile service: