Using Personalization with Anonymous Users


So far, the examples for storage of profile information require an authenticated user. In the last chapter, you learned that ASP.NET 2.0 can identify anonymous users. As long as this feature is activated, you can mark custom properties of the profile so that they can be saved even if the user is anonymous.

A typical example once more is the shopping basket. The user should be able to put products into a basket without registering first. As opposed to the normal storage into a session variable, which I've used so far in this context, anonymous users will be recognized at a later visit so that their shopping basket is still available.

Activating Profile Properties for Anonymous Users

Before profile information can be saved for anonymous users, the profile properties feature has to be activated. This happens as described in the previous chapter through the web.config configuration file:

 <?xml version="1.0"?> <configuration>     <system.web>         <anonymousIdentification             enabled="true"         />     </system.web> </configuration> 

Afterwards, for each property of the profile, you can define whether it should be stored for anonymous users or not. This has to be done with the allowAnonymous attribute. If the attribute isn't noted, the property won't be stored.

Based on the previous examples, it seems useful to make the shopping basket and bookmark functionality accessible to anonymous users. More personal data, such as the nickname and date of birth, are reserved for authenticated users.

 <personalization>     <profile>         <property name="Nickname" />         <property name="Birthday" type="System.DateTime" />         <property name="YearsExperienceASPNET" type="int" defaultValue="1" />         <property name="Bookmarks"             type="System.Collections.Specialized.StringCollection"             serializeAs="Xml"             allowAnonymous="true"         />         <property name="Basket"             type="Basket"             serializeAs="Xml"             allowAnonymous="true"         />     </profile> </personalization> 

I've copied the basket.aspx file to the nonprotected root directory of the web site to test its anonymous usage. As shown in Figure 7-7, users can edit their shopping basket even if they aren't authenticated (as evidenced by the Login link on the top-right side).

click to expand
Figure 7-7: Even anonymous users can store items in their personal shopping basket.

Migrating Anonymous Profiles to Authenticated Users

Migration is a crucial feature when it comes to storing profiles of anonymous users. Think again of the shopping basket. Let's say a user is putting all sorts of things into a basket and wants to register afterwards to place the order. Naturally, the complete contents of the shopping basket mustn't get lost.

Such a migration of an anonymous profile is possible, but has to be implemented manually. This makes sense because it's the only way that the developer can make the decision about the type and the data flow of the migration.

The migration can be handled in the MigrateAnonymous event of the class PersonalizationModule. This will typically happen in the global.asax file, which has to be added to the web site to fulfill this purpose. As you can see in the following example, you get access to the anonymous user ID by the event arguments as well as to the new profile. With the ID, you can request the old profile and take over the contents according to your wishes. In this case, the bookmarks as well as all the entries of the shopping basket are copied, as shown in Figure 7-8.

click to expand
Figure 7-8: The anonymous basket is merged with the one already saved for the authenticated user.

 <%@ application language="C#" %> <script runat="server"> void Personalization_MigrateAnonymous(object sender,     PersonalizationMigrateEventArgs e) {     HttpPersonalization oldProfile =          ((HttpPersonalization) e.Context.Profile).GetProfile(e.AnonymousId);     HttpPersonalization newProfile = (HttpPersonalization) e.Context.Profile;     // Merging Bookmarks     string[] bookmarks = new string[oldProfile.Bookmarks.Count];     oldProfile.Bookmarks.CopyTo(bookmarks, 0);     newProfile.Bookmarks.AddRange(bookmarks);     // Merging Basket     newProfile.Basket.AddRange(oldProfile.Basket); } </script>] 




ASP. NET 2.0 Revealed
ASP.NET 2.0 Revealed
ISBN: 1590593375
EAN: 2147483647
Year: 2005
Pages: 133

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