ASP.NET

Using Profiles

Create a new project. Name the project MakeItPersonal. Add a Web.Config file to the project. Update Web.Config to include some profile properties.

  1. The example here includes a user name, a Theme, and a birthdate. Be sure to turn anonymousIdentification to true. The following example shows that you may group and nest profile structures using the <group> element.

    <system.web>
    
        <profile>
          <properties >
            <add name="Theme" type="System.String"/>
            <add name="Name" type="String"/>
            <add name="Birthdate"" type="System.DateTime"/>
            <group name="Address">
                <add name="StreetAddress"/>
                <add name="City"/>
                <add name="State"/>
                <add name="ZipCode"/>
             </group>
          </properties>
        </profile>
    
    </system.web>
    
    NOTE
    Supporting Anonymous Personalization This example uses the authenticated user name as the key for locating personalization information. However, ASP.NET supports "anonymous" personalization. That is, ASP.NET supports personalization information for anonymous users-but tracks the users via a cookie. You may add support for anonymous personalization tracking by turning the anonymousIdentification element to "true" and specifying cookie parameters like this:
        <anonymousIdentification enabled="true"
        cookieName=".ASPXANONYMOUSUSER"
        cookieTimeout="120000"
        cookiePath="/"
        cookieRequireSSL="false"
        cookieSlidingExpiration="true"
        cookieProtection="Encryption"
        cookieless="UseDeviceProfile" />
    

    By configuring the site this way, ASP.NET will store the personalization settings based on a cookie it generates when a user first hits the site.

  2. Borrow the Default and SeeingRed Themes from the MasterPagesSite project (Tutorial 8). This will let the user pick the Theme.

  3. Borrow the UseThemes.aspx and .cs files from the MasterPagesSite project.

  4. Borrow the Banner.ascx file from the MasterPagesSite.

  5. Now update the Default.aspx page. This will be where users type profile information.

    Add text boxes for the name, address, city, state, and zip code.

    Add a drop-down list box populated with Default and SeeingRed items. This will be used for selecting the Theme.

    Also add a calendar control to pick the birthdate.

  6. Add a button the user may click to submit profile information. Add a handler to input these values into the profile. Double-click on the button to add the handler.

    The input screen should look something like this:

    Graphic
    NOTE
    Adding Users to Authenticate This example uses the authenticated user name as the key for storing personalization values. Use the ASP.NET Configuration Utility to apply Forms Authentication to this application (as described in tutorial 10). Also add at least one user so that you have one to personalize. Add a Login.ASPX screen to the site and modify the site's access rules to enforce authentication. Then you will be able to see the personalization information being stored and retrieved.
  7. Update Page_Load to display profile information (if it's there). Grab the profile object and set each of the text boxes and the calendar control.

    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class _Default : System.Web.UI.Page
    {
       protected void Page_Load(object sender, EventArgs e)
       {
          if (!this.IsPostBack)
    
         {
             ProfileCommon pc = this.Profile.GetProfile(Profile.UserName);
             if (pc != null)
             {
                 this.TextBoxName.Text = pc.Name;
                 this.TextBoxAddress.Text = pc.Address.StreetAddress;
                 this.TextBoxCity.Text = pc.Address.City;
                 this.TextBoxState.Text = pc.Address.State;
                 this.TextBoxZipCode.Text = pc.Address.ZipCode;
                 this.DropDownList1.SelectedValue = pc.Theme;
                 this.Calendar1.SelectedDate = pc.Birthdate;
             }
         }
       }
    //  …
    }
    
  8. Update the profile submission handler to store the profile information.

    protected void ButtonSubmitProfile_Click(object sender, EventArgs e)
    {
    
    ProfileCommon pc = this.Profile.GetProfile(Profile.UserName);
    
       if (pc != null)
       {
          pc.Name = this.TextBoxName.Text;
          pc.Address.StreetAddress = this.TextBoxAddress.Text;
          pc.Address.City = this.TextBoxCity.Text;
          pc.Address.State = this.TextBoxState.Text;
          pc.Address.ZipCode = this.TextBoxZipCode.Text;
          pc.Theme = this.DropDownList1.SelectedValue;
          pc.Birthdate = this.Calendar1.SelectedDate;
    
          pc.Save();
       }
    }
    
  9. Finally, update the UseThemes.aspx page to use the Theme. Override the page's OnPreInit method. Have the code apply the Theme as specified by the profile.

    protected override void OnPreInit(EventArgs e)
    {
       ProfileCommon pc = this.Profile.GetProfile(Profile.UserName);
          if (pc != null)
          {
             String strTheme = pc.Theme.ToString();
             if (strTheme != null &&
                 strTheme.Length > 0)
             {
                 this.Theme = strTheme;
             }
          }
      base.OnPreInit(e)
    }
    
  10. When you surf to the page, you should be able to enter the profile information and submit it. Following your initial visit, the profile will be available whenever you hit the site.


Conclusion

Profiles represent an effective way to add personalization to your site. The profile scheme in the Web.Config defines the profiles available to the application. ASP.NET will synthesize a ProfileCommon class that includes support for the properties defined in Web.Config. To access the properties, grab the Profile object from the Page for the current HttpContext. ASP.NET will take care of the details of serializing the property data and tracking it either anonymously or by using the identity of the logged in user.

Tutorial 12 Quick Reference

How to define personalization profile settings

Use the <profile> element in Web.Config. Define name/type pairs to create the profiles schema

How to access the profile properties

Profile properties are available through the page and through the current HttpContext

How to track the profiles with cookies

Enable anonymousIdentification in Web.Config