12.5. Anonymous Personalization


It is common to allow your users to personalize your site before identifying themselves. A classic example of this is Amazon.com, which lets you add books to your shopping cart before you log in (you only need to log in when you are ready to purchase what is in your cart).

ASP.NET 2.0 supports personalization for anonymous users as well as the ability later to link anonymous personalized data with a specific user's. Once that user logs in, you don't want to lose what was in the user's cart.

To enable anonymous personalization, you must update your Web.config file adding:

          <anonymousIdentification enabled="true" /> 

Add the attribute-value pair allowAnonymous="true" to the CHOSENBOOKS element of Web.config, as shown in Example 12-17.

Example 12-17. Modified Web.config for anonymous access
 <?xml version="1.0"?> <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">    <connectionStrings>       <remove name="LocalSqlServer"/>       <add name="LocalSqlServer" connectionString="data source=.\SqlExpress;Integrated Security=SSPI;Initial Catalog=aspnetdb"/>    </connectionStrings>    <system.web>       <anonymousIdentification enabled="true" />       <roleManager enabled="true" />       <authentication mode="Forms"/>       <membership defaultProvider="AspNetSqlMembershipProvider"/>       <compilation debug="true"/>       <profile enabled="True" defaultProvider="AspNetSqlProfileProvider">          <properties>             <add name="lastName" />             <add name="firstName" />             <add name="phoneNumber" />             <add name="birthDate" type="System.DateTime"/>             <add name="CHOSENBOOKS" allowAnonymous="true"              type="System.Collections.Specialized.StringCollection" />          </properties>       </profile>    </system.web> </configuration> 

Redesign your Welcome.aspx page in two ways: first move the hyperlink to the profile Information page outside of the Logged In template. Second move the listbox (lbBooks) outside the panel. Thus, you can see both of these features whether or not you are logged in. Also, change the text on the Add Profile Info hyperlink to just Profile Info, since you will be using this link to add and edit the profile info.

When an anonymous user fills in the profile information, the user will automatically be assigned a Globally Unique Identifier (GUID), and an entry will be made in the database for that ID. However, note that only those properties marked with allowAnonymous may be stored, so you must modify your Save_Click event handler in ProfileInfo.aspx.vb. Bracket the entries for all the profile elements except CHOSENBOOKS in an If statement that tests whether the user is currently Anonymous. The new save_Click event handler for ProfileInfo.aspx.vb is shown in Example 12-18.

Example 12-18. Modified Save_Click event handler for ProfileInfo.aspx.vb
 Protected Sub save_Click( _ ByVal sender As Object, _ ByVal e As System.EventArgs) Handles save.Click    If Profile.IsAnonymous = False Then       Profile.lastName = Me.lastName.Text       Profile.firstName = Me.firstName.Text       Profile.phoneNumber = Me.phone.Text       Profile.birthDate = CType(Me.birthDate.Text, System.DateTime)    End If    Profile.CHOSENBOOKS = New System.Collections.Specialized.StringCollection( )    For Each item As ListItem In Me.cblChosenBooks.Items       If item.Selected Then          Profile.CHOSENBOOKS.Add(item.Value.ToString( ))       End If    Next    Response.Redirect("Welcome.aspx") End Sub 

The effect of the new code shown in Example 12-18 is that you check whether the IsAnonymous property is false. If it is, then you are dealing with a logged-in user, and you can get all of the properties; otherwise, you can get only those that are allowed for anonymous users.

Modify the ProfileInfo page so that the non-anonymous data is in a panel that will be invisible for users who are not logged in. The simplest way to do this may be to switch to Source view and bracket the nonanonymous code inside a panel (don't forget to end the table before ending the panel), as shown in Example 12-19.

Example 12-19. Adding a nonanonymous information panel to ProfileInfo.aspx.vb
 <body>     <form  runat="server">     <div>         <asp:Panel  runat="server">             <table>             <tr>                 <td>First Name: </td>                 <td style="width: 193px">                   <asp:TextBox  Runat="server" />                 </td>             </tr>             <tr>                 <td>Last Name: </td>                 <td style="width: 193px">                  <asp:TextBox  Runat="server" /></td>             </tr>             <tr>                 <td>Phone number: </td>                 <td style="width: 193px">                    <asp:TextBox  Runat="server" />                 </td>             </tr>             <tr>                 <td>BirthDate</td>                 <td style="width: 193px">                    <asp:TextBox  Runat="server" />                 </td>             </tr>         </table>         </asp:Panel> 

Modify the Page_Load for ProfileInfo.aspx to hide the panel if the user is anonymous, as shown in Example 12-20.

Example 12-20. Modified page load ProfileInfo.aspx.vb
 Protected Sub Page_Load(ByVal sender As Object, _   ByVal e As System.EventArgs) Handles Me.Load    If Not IsPostBack And Profile.UserName IsNot Nothing Then       If Profile.IsAnonymous = True Then          Me.pnlNonAnonymousInfo.Visible = False       Else          Me.pnlNonAnonymousInfo.Visible = True          If Profile.IsAnonymous = False Then             Me.lastName.Text = Profile.lastName             Me.firstName.Text = Profile.firstName             Me.phone.Text = Profile.phoneNumber             Me.birthDate.Text = Profile.birthDate.ToShortDateString( )          End If          If Profile.CHOSENBOOKS IsNot Nothing Then             For Each theListItem As ListItem In Me.cblChosenBooks.Items                For Each theProfileString As String In Profile.CHOSENBOOKS                   If theListItem.Text = theProfileString Then                      theListItem.Selected = True                   End If                Next             Next          End If   'Profile.CHOSENBOOKS IsNot Nothing       End If      'Profile.IsAnonymous = True    End If         'Not IsPostBack And Profile.UserName IsNot Nothing End Sub 

Run the application. Do not log in, but do click the Profile Info link. Select a few books and click Save. When you return to the Welcome page, you are still not logged in, but your selected books are displayed, as shown in Figure 12-41.

Stop the application and reopen the database. You'll see that an ID has been created for this anonymous user (and the UserName has been set to the GUID generated). In addition, the profile information has been stored in the corresponding record, as shown in Figure 12-42.

Figure 12-41. Anonymous user information


Figure 12-42. Anonymous user record in database


12.5.1. Migrating the Anonymous Data to the Actual User's Record

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 the MigrateAnonymous event 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 not Nothing, 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 the CHOSENBOOKS collection.

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




Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net