Personalization is a key component of most Web applications these days. TechRepublic and Amazon are good examples of sites that remember certain user attributes. Providing such functionality with ASP.NET 1.x required some extra work and the use of the Session object, but version 2.0 simplifies the personalization process.
Profiling
The ASP.NET 2.0 Profile system allows you to easily store user-specific information. You define the properties that are associated with each user and stored in their profile. The profile data elements are easily accessed programmatically. As with any maintained data, it must be stored and profiles use SQL Server by default.
The Profile system is based on the provider model, and the particular Profile provider is responsible for serializing and deserializing the property values to some data store. The .NET Framework ships with a SqlProfileProvider class by default, which uses a SQL Server database table to store data.
System setup
Before you can use the profile features of ASP.NET 2.0, the Web server must be properly set up to support it. The profiling feature utilizes a SQL Server backend by default, but you can also create your own provider. In this article, I focus on the standard SQL Server backend, which utilizes the default SqlProfileProvider class.
SQL Server support includes version 7.0, 2000, and 2005. Configuring an environment to utilize SQL Server begins with the database server and ends with the ASP.NET platform.
The .NET Framework 2.0 includes tools to properly set up the ASP.NET environment. No configuration is required if you use the SQL Server 2005 Express Edition included with Visual Studio. If not, the first step is registering a SQL Server connection. This is accomplished with the command line tool Aspnet_regsql.exe installed with the .NET Framework. The tool's path on my development machine is:
C:\Windows\Microsoft.NET\v2.0.50727\aspnet_regsql.exe
The tool includes numerous options that you may peruse with the help command line option (-?). On my development system, I use an existing SQL Server 2000 installation. The following command sets up the environment by creating the necessary database on the specified server:
Aspnet_regsql.exe –A p –E –S database_server_name
The E command line switch tells the system to authenticate using the current user credentials. The S switch allows you to specify the database server name. The A switch signals what options are added with the accompanying p to specify adding profile support. A SQL Server database is created when the command is issued. The database's default name is aspnetdb, but you may override this with the d command line switch, which allows you to specify an existing database.
Now that the SQL Server environment is established, the next step is configuring ASP.NET 2.0 to utilize the connection and profiling. The properties of the ASP.NET environment are available via the IIS administrative interface by viewing the IIS Web Sites properties and selecting the ASP.NET tab. Select the Edit Configuration button on the ASP.NET tab. The Authentication tab includes a Roles area with Role Management enabled (this should be checked). The default SQL Server (LocalSqlServer) connection string is specified in the General tab. The following connection string is used for my development environment:
data source= database_server_name;Integrated Security=SSPI;
Initial Catalog=aspnetdb;User Instance=false
Saving these changes to the ASP.NET environment links it to the previously configured SQL Server instance. With the platform configured, you can move forward with implementation.
Defining the elements
The data elements used in the profile are created and maintained in the web.config file (i.e., the data values to be maintained are defined in that file). The actual values (for different users) are maintained in a backend data store. For each property, you can specify the name, data type, and how the data should be serialized.
The data types follow the standard data types available in .NET. The serialization option will usually depend upon the type of data being stored, but you have four serialization options:
- ProviderSpecific: the default Profile provider determines how to serialize the property values.
- String: the value is converted to a string.
- Xml: the value is converted to an XML representation.
- Binary: the value is converted to its binary representation.
The profile elements are defined in a properties element contained in a profile element, which resides in the system.web section of the web.config file. The web.config in Listing A includes a profile section to utilize the previously configured profile support. It will use two profile items—FirstName and LastName—that store string values and allow anonymous users.
The profile feature may be utilized with anonymous as well as authenticated users. The previous web.config file specifies anonymous support using both the allowAnonymous attribute of profile element along with enabling anonymous support for the application (anonymousIdentification element).
Using code
Once the environment is properly configured, it is easy to use profile elements in your .NET code. The simple C# example in Listing B uses profile elements to store and display data values. It takes advantage of the Profile class contained in the System.Web namespace. The code includes the ASP.NET file following the code behind file.
Listing C contains the codebehind for the custom code. The first button (btnSet) stores the values from each text field in the profile fields. The second button retrieves the values from the profile fields and populates the text fields with these values.
Personalization is simpler
Personalization is just one of the many new features available in ASP.NET 2.0. After a few configuration steps, you can easily add personalization features to your applications through the standard web.config file and coding via the Profile class.
Miss a column?
Check out the .NET Archive, and catch up on the most recent editions of Tony Patton's column.
Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.



