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

Personalization

Providing default values

In addition to defining the types of personalization properties, you can also define their default values. The personalization properties you create do not have a value, but you can easily change this using the DefaultValue attribute of the <add> element. Defining default values is illustrated in Listing 9-8.

Listing 9-8: Defining default values for personalization properties

<properties>

<add name=”FirstName” type=”System.String” /> <add name=”LastName” type=”System.String” /> <add name=”LastVisited” type=”System.DateTime” /> <add name=”Age” type=”System.Integer” />

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

</properties>

From this example, you can see that only one of the personalization properties is provided with a default value. The last personalization property, Member, in this example, is given a default value of False. This means that when you add a new end user to the personalization property database, Member is defined instead of remaining a blank value.

Making personalization properties read-only

It is also possible to make personalization properties read-only. To do this, you simply add the readOnly attribute to the <add> element:

<add name=”StartDate” type=”System.DateTime” readOnly=”True” />

To make the personalization property a read-only property, you give the readOnly attribute a value of True. By default, this property is set to False.

Anonymous Personalization

A great new feature in ASP.NET 2.0 enables anonymous end users to utilize the personalization features it provides. This is important if a site requires registration of some kind. In these cases, end users do not always register for access to the greater application until they have first taken advantage of some of the basic services. For instance, many e-commerce sites allow anonymous end users to shop a site and use the site’s shopping cart before the shoppers register with the site.

Enabling anonymous identification of the end user

By default, anonymous personalization is turned off because it consumes database resources on popular sites. Therefore, one of the first steps to allow anonymous personalization is to turn on this feature using a setting in the web.config file.

275

Chapter 9

As shown in Listing 9-9, you can turn on anonymous identification to enable the personalization engine to identify the unknown end users.

Listing 9-9: Allowing anonymous identification

<configuration>

<system.web>

<anonymousIdentification enabled=”True” />

</system.web>

</configuration>

To enable anonymous identification of the end users who might visit your applications, you add an

<anonymousIdentification> element to the web.config file within the <system.web> nodes. Then within the <anonymousIdentification> element, you use the Enabled attribute and set its value to True. Remember that by default, this value is set to False.

When anonymous identification is turned on, ASP.NET uses a unique identifier for each anonymous user who comes to the application. This identifier is sent with each and every request, although after the end user becomes authenticated by ASP.NET, the identifier is removed from the process.

For an anonymous user, information is stored by default as a cookie on the end user’s machine. Additional information (the personalization properties that you enable for anonymous users) is stored in the specified data store on the server.

Changing the name of the cookie for anonymous identification

Cookies are used by default under the cookie name .ASPXANONYMOUS. You can change the name of this cookie from the <anonymousIdentification> element in the web.config file by using the cookieName attribute, as shown in Listing 9-10.

Listing 9-10: Changing the name of the cookie

<configuration>

<system.web>

<anonymousIdentification

enabled=”True” cookieName=”.ASPXEvjenWebApplication” />

</system.web>

</configuration>

Changing the length of time the cookie is stored

Also, by default, the cookie stored on the end user’s machine is stored for 100,000 minutes (which is almost 70 days). If you want to change this value, you do it within this <anonymousIdentification> element through the use of the cookieTimeout attribute, as shown in Listing 9-11.

276

Personalization

Listing 9-11: Changing the length of time the cookie is stored

<configuration>

<system.web>

<anonymousIdentification

enabled=”True” cookieTimeout=”1440” />

</system.web>

</configuration>

In this case, I changed the cookieTimeout value to 1440 — meaning 1,440 minutes (or one day). This would be ideal for a shopping cart for which you don’t want to persist the identification of the end user too long.

Changing how the identifiers are stored

Although anonymous identifiers are stored through the use of cookies, you can also easily change this. Cookies are, by far, the preferred way to achieve identification, but you can also do it without the use of cookies. Other options include using the URI or device profiles. Listing 9-12 shows an example of using the URI to place the identifiers.

Listing 9-12: Specifying how cookies are stored

<configuration>

<system.web>

<anonymousIdentification

enabled=”True” cookieless=”UseUri” />

</system.web>

</configuration>

Besides UseUri, other options include UseCookies, AutoDetect, and UseDeviceProfile. The following list reviews each of the options:

UseCookies: This is the default setting. If you set no value, ASP.NET assumes this is the value. UseCookies means that a cookie is placed on the end user’s machine for identification.

UseUri: This value means that a cookie will not be stored on the end user’s machine, but instead the unique identifier will be munged within the URL of the page. This is the same approach used for cookieless sessions in ASP.NET 1.0/1.1. Although this is great if developers want to avoid sticking a cookie on an end user’s machine, it does create strange looking URLs and can be an issue when an end user bookmarks pages for later retrieval.

AutoDetect: Using this value means that you are letting the ASP.NET engine decide whether to use cookies or use the URL-approach for the anonymous identification. This is done on a peruser basis and performs a little worse than the other two options. ASP.NET must check the end user before deciding which approach to use. My suggestion is to use AutoDetect instead of

277

Chapter 9

UseUri if you absolutely must allow for end users who have cookies turned off (which is rare these days).

UseDeviceProfile: Configures the identifier for the device or browser that is making the request.

Looking at how the anonymous identifiers are stored

In order to make the anonymous identifiers unique, a globally unique GUID is used. You can also now grab hold of this unique identifier for your own use. In order to retrieve the GUID, the Request object has been enhanced with an AnonymousId property. The AnonymousId property returns a value of type String, which can be used in your code as shown here:

Label1.Text = Request.AnonymousId

Working with anonymous identification events

In working with the creation of anonymous users, be aware of two important events that can be used for managing the process:

AnonymousIdentification_OnCreate

AnonymousIdentification_OnRemove

By using the AnonymousIdentification_OnCreate event, you can work with the identification of the end user as it occurs. For instance, if you do not want to use GUIDs for uniquely identifying the end user, you can change the identifying value from this event instead.

To do so, create the event using the event delegate of type AnonymousIdentificationEventArgs, as illustrated in Listing 9-13.

Listing 9-13: Changing the unique identifier of the anonymous user

VB

Public Sub AnonymousIdentification_OnCreate(ByVal sender As Object, _ ByVal e As AnonymousIdentificationEventArgs)

e.AnonymousId = “Bubbles “ & DateTime.Now()

End Sub

C#

public void AnonymousIdentification_OnCreate(object sender, AnonymousIdentificationEventArgs e)

{

e.AnonymousId = “Bubbles “ + DateTime.Now();

}

The AnonymousIdentificationEventArgs event delegate exposes an AnonymousId property that assigns the value used to uniquely identify the anonymous user. Now, instead of a GUID to uniquely identify the anonymous user as

d13fafec-244a-4d21-9137-b213236ebedb

278