May 21, 2019
Hot Topics:

ASP.NET 2.0 Profile: Simple User Personalization for Your Web Apps

  • July 15, 2005
  • By Thiru Thangarathinam
  • Send Email »
  • More Articles »

Defining Profile Groups

Sometimes you may want to group the related properties into a profile group so that you can easily work with them from your ASP.NET page. For example, the following profile declaration creates a group named UserPreferences that is made up of properties named BackGroundColor, ForeColor, and FontSize:


<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">   




  <group name="UserPreferences">

    <add name="BackgroundColor" type="System.String"/>

    <add name="ForeColor" type="System.String"/>

    <add name="FontSize" type="System.Int16"/>









Note that you can have multiple profile group declarations inside the <profile> element. The next section shows how to access the UserPreferences profile group from within an ASP.NET page.

Working with Profile Groups from an ASP.NET Page

The ASP.NET page shown below has two command buttons that drive the functionality of the page:


<%@ Page Language="C#" %>

<script runat="server">

void btnGetProfileValues_Click(object sender, EventArgs e)


    lblBackground.Text = Profile.UserPreferences.BackgroundColor;

    lblForeColor.Text = Profile.UserPreferences.ForeColor;

    lblFontSize.Text = Profile.UserPreferences.FontSize.ToString();

    Panel1.BackColor = System.Drawing.Color.FromName

    Panel1.ForeColor = System.Drawing.Color.FromName


    Panel1.Font.Size = FontUnit.Parse




void btnSetValues_Click(object sender, EventArgs e)


    Profile.UserPreferences.BackgroundColor = txtBackColor.Text;

    Profile.UserPreferences.ForeColor = txtForeColor.Text;

    Profile.UserPreferences.FontSize =




<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

  <title>Accessing Profile Groups from an ASP.NET Page</title>



<form id="form1" runat="server">


    BackColor: <asp:TextBox runat="server" ID="txtBackColor"/><br/>

    ForeColor: <asp:TextBox runat="server" ID="txtForeColor"/><br/>

    Font Size: <asp:TextBox runat="server" ID="txtFontSize"/><br/> 

    <asp:Button runat="server" ID="btnSetName"

      Text="Set Profile Values" OnClick="btnSetValues_Click"/><br/>


    <asp:Panel ID="Panel1" runat="server" Height="224px"

      Width="265px" BorderColor="LightGray" BorderStyle="Dotted">

      <asp:Button runat="server" ID="btnGetProfileValues"

        Text="Get Profile Values" OnClick="btnGetProfileValues_Click" />       


      Profile Values: <br />

      Back Color:&nbsp;<asp:Label runat="server" ID="lblBackground"/><br/>

      Fore Color: <asp:Label runat="server" ID="lblForeColor"/><br/>

      Font Size: <asp:Label runat="server" ID="lblFontSize"/>







When the first command button (named btnSetValues) is clicked, the page sets the values of the Profile.UserPreferences group to appropriate values. When the second command button (named btnGetProfileValues) is clicked, it simply retrieves the values of the Profile.UserPreferences group and displays them in label controls. In addition, it sets various properties of the Panel control, such as BackColor, ForeColor, and Size, to the values retrieved from the Profile object.


Accessing the profile group properties is very similar to accessing the regular profile properties. The only difference is you need to fully qualify the profile property with the profile group name, as shown below:


    Profile.UserPreferences.BackgroundColor = txtBackColor.Text;

    Profile.UserPreferences.ForeColor = txtForeColor.Text;


If you request the page from the browser, set the values of the Profile.UserPreferences group using the "Set Profile Values" and then hit the "Get Profile Values" command button. You should see an output somewhat similar to Figure 3.


Figure 3. Output from “Get Profile Values”


As you can see from the above output, the Panel control’s related properties are assigned from the profile object.

Using Profiles with Anonymous Users

The previous examples explained how to utilize the profile feature with users who log in using integrated Windows authentication. Sometimes you will need to store data in the profile object for anonymous users as well. To accomplish this, you must first enable another ASP.NET 2.0 feature: anonymous identification. You also must mark properties in the <profile> section with a special boolean attribute: allowAnonymous. The following code demonstrates how to set up the web.config file to allow for anonymous users:


<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">   


  <anonymousIdentification enabled="true"/>         



        <add name="Name" allowAnonymous="true" type="System.String"/>

 <add name="UserCount" allowAnonymous="true" type="System.Int16"/>        








Choosing Profile Providers

As mentioned before, ASP.NET 2.0 ships with two providers: SQL Server Express and SQL Server. The default provider is SQL Server Express. To change to the SQL Server provider (or your own), use the ASP.NET Web Application Administration tool. Under the Provider tab, click "Select a different provider for each feature (advanced)" and then select the relevant provider in the Profile Provider box.


In addition to the default providers, you might need to create and use a custom profile provider (e.g., you already have a database that stores user information, such as an employee database, or you need to use a database other than SQL Server or in some other format such as XML files). ASP.NET 2.0 also provides the ability to serve the properties stored in a user profile through different profile providers. This provides you the flexibility to manage data from different profile providers from multiple data sources for users of your application using a single user profile.


Design Personalization-Related Features

Profile is a very useful feature in ASP.NET 2.0. The object enables you to automatically store user information across multiple visits to a Web application by providing a generic storage system. You can store any type of information within a user profile, including both simple data types such as strings and integers and complex types such as custom objects.


If you need to design personalization-related features in your Web application, you will find that the new ASP.NET 2.0 Profile object will greatly simplify the design and implementation.

Download the Code

To download the accompanying source code for the examples, click here.

About the Author

Thiru Thangarathinam has many years of experience in architecting, designing, developing, and implementing applications using object-oriented application development methodologies. His certifications include MCAD for .NET, MCSD, and MCP. Thiru is an expert in ASP.NET, .NET Framework, Visual C# .NET, Visual Basic .NET, ADO.NET, XML Web services, and .NET remoting. Thiru also has authored numerous books and articles. Contact him at thiruthangarathinam@yahoo.com.              



Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date