Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [159]

By Root 5775 0
through the user role. The static method Create on the ProfileBase class takes the user name and creates an instance of the profile object specific to that user. ProfileCommon is the common name of the dynamically created class that contains the user profile.

The handler of the Personalize event receives data through the ProfileEventArgs class. The class has a read-write member named Profile. When the event handler returns, the profile HTTP module checks this member. If it is null, the module proceeds as usual and creates a profile object based on the user’s identity. If not, it simply binds the current value of the Profile member as the profile object of the page.

Migrating Anonymous Data


As mentioned, anonymous users can store and retrieve settings that are persisted using an anonymous unique ID. However, if at a certain point a hitherto anonymous user decides to create an account with the Web site, you might need to migrate to her account all the settings that she made as an anonymous user. This migration doesn’t occur automatically.

When a user who has been using your application anonymously logs in, the personalization module fires an event—MigrateAnonymous. Properly handled, this global event allows you to import anonymous settings into the profile of an authenticated user. The following pseudocode demonstrates how to handle the migration of an anonymous profile:

void Profile_MigrateAnonymous(object sender, ProfileMigrateEventArgs e)

{

// Load the profile of the anonymous user

ProfileCommon anonProfile;

anonProfile = Profile.GetProfile(e.AnonymousId);

// Migrate the properties to the new profile

Profile.UseEuroMetricSystem = anonProfile.UseEuroMetricSystem;

...

}

You get the profile for the anonymous user and extract the value of any property you want to import. Next you copy the value to the profile of the currently logged-on user.

Profile Providers


In ASP.NET, the profile API is composed of two distinct elements: the access layer and the storage layer.

The access layer provides a strongly typed model to get and set property values and also manages user identities. It guarantees that the data is retrieved and stored on behalf of the currently logged-on user.

The second element of the profile system is data storage. The system uses ad hoc providers to perform any tasks involved with the storage and retrieval of values. ASP.NET comes with a profile provider that uses SQL Server Express as the data engine. If necessary, you can also write custom providers. The profile provider writes profile data into the storage medium of choice and is responsible for the final schema of the data.

Important

In ASP.NET, a provider is defined as a pluggable component that extends or replaces some system functionality. The profile provider is just one implementation of the ASP.NET provider model. Other examples of providers are the membership provider and role manager provider, both of which will be discussed later in the book. At its core, the provider infrastructure allows customers to change the underlying implementation of some out-of-the-box system functionalities while keeping the top-level interface intact. Providers are relatively simple components with as few methods and properties as possible. Only one instance of the provider exists per application domain.

Configuring Profile Providers


All features, such as user profiling, that have providers should have a default provider. Normally, the default provider is indicated via a defaultProvider attribute in the section of the configuration file that describes the specific feature. By default, if a preferred provider is not specified, the first item in the collection is considered the default.

The default profile provider is named AspNetSqlProfileProvider and uses SQL Server Express for data storage. Providers are registered in the section of the configuration file under the main node , as shown here:

connectionStringName="LocalSqlServer" applicationName="/"

Return Main Page Previous Page Next Page

®Online Book Reader