ASP.NET

Using Personalization

Using personalization is pretty straightforward. You basically define personalization properties in Web.Config. ASP.NET will synthesize a class you may use to manage personalization settings. At that point, profile information is available in much the same way as session state is available.

Defining Profiles in Web.Config

Profile schema is defined within Web.Config as name/type pairs. Imagine that in the course of designing your site, you decided you'd like to track the following information about a particular user:

  • User name

  • Gender

  • Visit count

  • Birthday

Defining these properties is a matter of populating them in Web.Config. A definition for the properties listed above might look like this Web.Config:

<system.web>
   <profile automaticSaveEnabled="true" >
      <properties>
         <add name="NumVisits" type="System.Int32"/>
         <add name="UserName" type="System.String"/>
         <add name="Gender" type="bool">
         <add name="Birthday" type="System.DateTime">
      </properties>
   </profile>
</system.web

The personalization properties consist of name/type pairs and will basically become the schema under which the personalization data will be stored. Once defined in the Web.Config file, the profile may be used in the site through the Profile property found in the current HttpContext (and is also available via the Page).

Use Profile Information

To use the profile in the Web site, you access it in much the same way you might access session state. However, instead of being represented by name/value pairs accessed through an indexer, the ASP.NET compiler will synthesize a profile object based upon the scheme defined in the Web.Config file.

For example, given the schema listed above, ASP.NET will synthesize a class named ProfileCommon, based upon the ProfileBase class. The synthesized class will reflect the instructions written into the Web.Config by inserting properties, shown here in bold:

public class ProfileCommon : ProfileBase
{
   public  virtual  HttpProfile GetProfile(string username);
   public  object GetPropertyValue(string propertyName);
   public  void SetPropertyValue(string propertyName,
              object propertyValue);
   public  HttpProfileGroupBase GetProfileGroup(String groupName);
   public  void Initialize(String username,Boolean isAuthenticated);
   public  virtual void Save();
   public  void Initialize(SettingsContext context,
             SettingsPropertyCollection properties,
             SettingsProviderCollection providers);
   public string UserName{get; set;};
   public int NumVisits{get; set;};
   public bool Gender(get; set; );
   public DateTime Birthdate{get; set; };
}

To access the profile properties, simply use the Profile property within the page. The Profile property is an instance of the ProfileCommon class synthesized by ASP.NET. Just access the members of the Profile, like so:

protected void Page_Load(object sender, EventArgs e)
{
    if (Profile.Name != null)
    {
        Response.Write("Hello " + Profile.Name);
        Response.Write("Your birthday is " +
              Profile.Birthdate);
    }
}

Saving Profile Changes

The preceding code snippet assumes there's already personalization information associated with the user. To insert profile data for a particular user, simply set the properties of the Profile object. For example, imagine a page that includes a handler for saving the profile. It might look something like this:

protected void ProfileSaveClicked(object sender, EventArgs e)
{
    Profile.Name = this.TextBoxName.Text;
    Profile.Birthdate = this.Calendar1.SelectedDate;
}

The easiest way to ensure that the personalization properties persist is to set the automaticSaveEnabled to true. Personal profile data will be saved automatically by the provider. Alternatively, you may call Profile.Save as necessary to save the personalization properties. In addition to saving and loading profiles, you may also delete the profile for a specific user by calling Profile.DeleteProfile.

Profiles and Users

Profile information is associated with the current user based upon the identity of the user. By default, ASP.NET uses the User.Identity.Name within the current HttpContext as the key to store data. By default, profiles are available only for authenticated users.

ASP.NET supports anonymous profiles as well. Turn this on within Web.Config. The default tracking mechanism for anonymous profiles is to use cookies. However, as with tracking session state, you may tell ASP.NET to use a mangled URL.

The following exercise illustrates using personalization profiles based on the user's login ID.