Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET 2.0 Beta Preview - B. Evjen.pdf
Скачиваний:
26
Добавлен:
24.05.2014
Размер:
15.33 Mб
Скачать

Personalization

the AnonymousID property is changed within the AnonymousIdentification_OnCreate event to

Bubbles 6/10/2004 2:07:33 PM

The AnonymousIdentification_OnRemove event also employs an event delegate of type Anonymous IdentificationEventArgs that is used immediately prior to migrating anonymous users to registered users. Note that the AnonymousId property of the Request object is still accessible at this point.

Anonymous options for personalization properties

Now that the capability to work with anonymous users is in place, you have to specify which personalization properties you want to enable for anonymous users. This is also done through the web.config file by adding the allowAnonymous attribute to the <add> element (see Listing 9-14).

Listing 9-14: Turning on anonymous capabilities personalization properties

<properties>

<add name=”FirstName” type=”System.String” /> <add name=”LastName” type=”System.String” />

<add name=”LastVisited” type=”System.DateTime” allowAnonymous=”true” /> <add name=”Age” type=”System.Integer” />

<add name=”Member” type=”System.Boolean” />

</properties>

In this example, the LastVisited property is set to allow anonymous users by setting the allow Anonymous attribute to True. Because this is the only property that works with anonymous users, the rest of the defined properties do not store information for these types of users.

Migrating Anonymous Users

When working with anonymous users, you must be able to migrate anonymous users to registered users. For instance, after an end user fills a shopping cart, she can then register on the site to purchase the items. At this moment, she switches from an anonymous user to a registered user.

For this reason, ASP.NET 2.0 provides a Profile_MigrateAnonymous event enabling you to migrate anonymous users to registered users. The Profile_MigrateAnonymous event requires an event delegate of type ProfileMigrateEventArgs. It is placed either in the page that deals with the migration or within the Global.asax file (if it can be used from anywhere within the application). The use of this event is illustrated in Listing 9-15.

Listing 9-15: Migrating anonymous users for particular personalization properties

VB

Public Sub Profile_MigrateAnonymous(ByVal sender As Object, _

ByVal e As ProfileMigrateEventArgs)

(continued)

279

Chapter 9

Listing 9-15: (continued)

Profile.LastVisited = Profile.GetPropertyValue(e.AnonymousId).LastVisited

End Sub

C#

public void Profile_MigrateAnonymous(object sender, ProfileMigrateEventArgs e)

{

Profile.LastVisited = Profile.GetPropertyValue(e.AnonymousId).LastVisited;

}

From this example, you populate the new Profile property with the old property. You get at the old property of the anonymous user by using the GetPropertyValue property, which takes a parameter of the ID of the anonymous user. From the Profile_MigrateAnonymous event, you still have access to the AnonymousId property, which you can retrieve from the event delegate — ProfileMigrateEventArgs.

Listing 9-15 shows how to migrate a single personalization property from an anonymous user to the new registered user. In addition to migrating single properties, you also must migrate properties that come from personalization groups. This is shown in Listing 9-16.

Listing 9-16: Migrating anonymous users for items in personalization groups

VB

Public Sub Profile_MigrateAnonymous(ByVal sender As Object, _

ByVal e As ProfileMigrateEventArgs)

Dim au As HttpProfile = Profile.GetProfile(e.AnonymousId)

If au.MemberDetails.DateJoined <> “” Then Profile.MemberDetails.DateJoined = DateTime.Now().ToString() Profile.FamilyDetails.MarriedStatus = au.FamilyDetails.MarriedStatus

End If

End Sub

C#

public void Profile_MigrateAnonymous(object sender, ProfileMigrateEventArgs e)

{

HttpProfile au = Profile.GetProfile(e.AnonymousId);

if (au.MemberDetails.DateJoined != String.Empty) { Profile.MemberDetails.DateJoined = DateTime.Now.ToString(); Profile.FamilyDetails.MarriedStatus = au.FamilyDetails.MarriedStatus;

}

}

Using this event either in the page or in the Global.asax file enables you to logically migrate anonymous users as they register themselves with your applications. The migration process also allows you to pick and choose which items you migrate and to change the values as you wish.

280