Profiles


Many commercial Websites let you can add products to a shopping cart, but if you take a coffee break before submitting your order, the session times out. The lesson to learn from this is that it's not a good idea to store orders only in session variables because session state is lost after the session timeout occurs.

ASP.NET 2.0 has a new concept for remembering user information persistently: profiles. Profiles are stored in the database, so they are also kept in cases where sessions end.

Profiles have the following characteristics:

  • Profiles are stored persistently. The data store is managed by a profile provider. ASP.NET 2.0 includes profile providers for SQL Server and Access. Custom profile providers can be developed.

  • Profiles are strongly typed. Session and application variables use an indexer, and casting is required to access the data. When you use profiles, properties with the name of the profile are generated automatically. The name and type of the properties are defined in the configuration file.

  • Profiles can be used with both anonymous and authenticated users. With anonymous users the user identification can be stored inside the cookie. As soon as the user logs on to the Website, the state from the anonymous user can be converted to the state for the authenticated user.

In the next Try It Out, you create and use profile information.

Try It Out – Create Profile Information

image from book
  1. Open the previously created Web application.

  2. Open the file web.config that was created in the previous chapter. If you don't see a web.config file the Solution Explorer, create a new one by selecting the menu option Add New Item and select Web Configuration File.

  3. Add the <profile> XML element section containing the Country, Visits and LastVisit properties to the file web.config, as shown. the Country property is of type String (this is the default), the property Visits is of type Int32, and the property LastVisit is of type DateTime.

    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">    <system.web>    <!-- ... -->     <profile> <properties> <add name="Country" /> <add name="Visits" type="System.Int32" defaultValue="0" /> <add name="LastVisit" type="System.DateTime" /> </properties> </profile>         <!-- ... -->   </system.web> </configuration>

  4. Now, create a new Web page, ProfileDemo.aspx, and add three labels to display current values and a drop-down list to select a country.

  5. Add the following code to the Page_Load() event handler to display current values from the profile. Profile is a dynamically created property of the Page class that has properties defined within the configuration file. These properties are strongly typed — the name and type of the properties are the same as those defined in the configuration file.

    void Page_Load(object sender, EventArgs e) { LabelLastVisit.Text = Profile.LastVisit.ToLongTimeString(); LabelVisitCount.Text = Profile.Visits.ToString(); LabelSelectedCountry.Text = Profile.Country; Profile.Visits++; Profile.LastVisit = DateTime.Now; }

  6. Now you can start and view the Web page. Select a country from the list. Close the browser and start it again. You will see that profile information is remembered between sessions.

How It Works

The Profile property that you can use within the code of a Web page is dynamically created. You cannot find this property in the Page class in the MSDN documentation, because this property does not exist as part of the Page class. Instead because of the property configuration in the web.config configuration file, the Profile property is created dynamically. The type of the Profile property is a dynamically created class that derives from ProfileBase and contains properties as defined with the configuration file.

image from book

Profile Groups

Profiles can keep track of a lot of information, and that information can be grouped. A group of properties is defined with the element <group>, as shown here:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">     <system.web>     <profile>       <properties>         <add name="Country" />         <add name="Visits" type="System.Int32" defaultValue="0" />         <add name="LastVisit" type="System.DateTime" /> <group name="EventSelection" > <add name="Country" /> <add name="City" /> <add name="StartDate" type="System.DateTime" /> <add name="EndDate" type="System.DateTime" /> </group>       </properties>     </profile>         <!-- ... -->    </system.web> </configuration>

So that you can access profile values that are defined within groups, a dynamic property is created that can be used to access the property values:

 string country = Profile.EventSelection.Country; string city = Profile.EventSelection.City; DateTime startDate = Profile.EventSelection.StartDate; DateTime endDate = Profile.EventSelection.EndDate; 

Profiles with Components

Inside the code of components, profile information cannot be accessed with a Profile property. Instead, the profile is accessed with the help of HttpContext. Also, strong typing is not available. The example shows the accessing of the Country profile string defined earlier. Current is a static property of the HttpContext class that returns an HttpContext object that is associated with the client session. The property Profile returns an object of type ProfileBase, where every profile value can be accessed using an indexer. The indexer is defined to return objects of type Object, so a cast is needed to write the value to a variable.

 string country = (string)HttpContext.Current.Profile["Country"]; 

Profiles with Custom Data Types

With profiles, you can store not only base data types such as int, short, string, and DateTime but also custom data types. The data type just has to support one serialization mechanism.

The following list explains the serialization mechanisms that are supported with profiles:

  • XML Serialization: XML serialization is the default. With XML serialization all public properties and fields are stored. This serialization mechanism requires a default constructor. XML serialization is described in Chapter 23.

  • Binary Serialization: With binary serialization the class must be marked with the attribute [Serializable]. Here, all private fields are serialized. Runtime serialization is described in Chapter 22.

  • String Serialization: With string serialization a type converter class can be used to serialize the data. If there's no type converter class to do the serialization, the ToString() method is invoked to serialize the object as a string.

The serialization that is used is defined with the serializeAs attribute as shown. If this attribute is not set, XML serialization is used. The possible options with the serializeAs attribute are Binary, Xml, and String.

<profile>   <properties> <add name="Demo" type="MyClass" serializeAs="Binary" />   </properties> </profile>

Profiles with Anonymous Users

By default, profiles are only enabled for authenticated users. To enable profiles for anonymous users, anonymous users must be enabled in the configuration file, and every profile property that should be available for anonymous users must be set individually.

<configuration>   <system.web> <anonymousIdentification enabled="true" /> <profile>   <properties> <add name="Country" allowAnonymous="true" />   </properties> </profile>

When a profile is accessed for an anonymous user, the anonymous user is created in the database of the profile provider. The anonymous user gets an ID similar to an authenticated user. The ID of the anonymous user by default is stored inside a cookie. However, it is also possible to use anonymous user identifiers within URL strings (URL mungling) instead of cookies.

Often anonymous profile information must be migrated to authenticated users. For example, a user might be able to add products to a shopping basket without being logged on, but to send the order, the user must log on to the system. In such a case all the profile information that was stored for the anonymous user must be migrated to the authenticated user.

For dealing with anonymous profiles, the following events can be acted on in global.asax.

Handler Name

Description

AnonymousIdentification_Create

When an anonymous user accesses the Website for the first time, an anonymous profile is created. This time the event handler AnonymousIdentification_ Create is invoked.

AnonymousIdentification_Remove

When an authenticated user accesses the Web page, and the request of the authenticated user contains a profile identifier of an anonymous user, the anonymous user profile is removed and the event handler AnonymousIdentification_Remove is invoked.

Profile_MigrateAnonymous

After the AnonymousIdentification_Remove event handler, the event handler Profile_MigrateAnonymous is invoked. With this event handler an anonymous profile can be migrated to a profile for an authenticated user.

The implementation of the handler Profile_MigrateAnonymous in global.asax can look like the following sample. All profile values that are allowed for anonymous users can be migrated to the authenticated user configuration. The sample code migrates the Country profile value that is allowed for anonymous users in the configuration file. First Profile.Country is checked to determine whether it already has a value. Profile.Country will have a value if the user was already logged on to the system earlier when the profile value was created. It will not have a value if the user has been anonymous to the system before. Here, the method GetProfile() passing an ID of a user helps to access profile information from other users. Although the same person sits on the client, an authenticated user and an anonymous user always have different user IDs. When an anonymous user logs on the system and is now an authenticated user, the ID of the anonymous user is passed with the argument ProfileMigrateEventArgs of the method Profile_MigrateAnonymous(). The anonymous user ID (e.AnonymousId) is passed to the method GetProfile() to access the profile of the anonymous user. This profile information is now migrated to the profile information of the authenticated user by assigning profile value by value (such as the Country in the example).

 void Profile_MigrateAnonymous(object sender, ProfileMigrateEventArgs e) { if (Profile.Country == null) { Profile.Country = Profile.GetProfile(e.AnonymousId).Country; } } 




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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