User Profile
At the most abstract level, a user configuration file is a set of attributes organized by the ASP. NET Runtime Library to a dynamically generated class. The configuration file data is saved by different users. When the program running page is displayed, ASP. NET dynamically creates the configuration file object, which contains the corresponding types of attributes defined by the developer in the data model. This object is added to the httpcontext object and can be obtained through the profile attribute.
Data Model Definition
To use the ASP. NET configuration file, you must first determine the structure of the data model to be used, and then attach the data model to the page through the configuration file. The layout of the user configuration file is defined in the web. config file.
Sample Code:
<system.web>
......
<profile>
<properties>
<add name="BackColor" type="string" />
<add name="ForeColor" type="string" />
</properties>
</profile>
</system.web>
All attributes added through the <add> label will become members of the dynamically created class.
The user configuration in the configuration file will automatically generate a class, as shown below:
namespace ASP
{
public class ProfileCommon : ProfileBase
{
public virtual string BackColor
{
get { (string)GetPropertyValue("BackColor"); }
set { SetPropertyValue("BackColor", value); }
}
public virtual string ForeColor
{
get{ (string)GetPropertyValue("ForeColor"); }
set{ SetPropertyValue("ForeColor", value); }
}
public virtual ProfileCommon GetProfile(string username)
{
object o = ProfileBase.Create(username);
return (ProfileCommon)o;
}
......
}
}
The instance of this type is automatically associated with the profile attribute of the page class. We can access the attributes of the user configuration file through the syntax such as profile. backcolor.
Use of Collection types
In the above example, we use a Single Scalar Value, and then the user configuration file supports more advanced features-set type and custom type. Let's take a look at the collection type:
<properties>
<add name="Links"
type="System.Collections.Specialized.StringCollection" />
</properties>
Non-standard values must be serialized according to the requirements of storage media. The serializeas attribute is used to indicate how serialization is performed. The optional values of this attribute include string, XML, binary, and providerspecific. If this attribute value is not specified, the default type is string.
Use of custom types
As long as the custom type is serializable, it can be used by the user configuration file. We can design the class by ourselves, compile it into an assembly, and then add the Assembly name to the type information of the configuration file attribute:
<properties>
<add name="ShoopingCart"
type="My.Namespace.DataContainer, MyAssembly" />
</properties>
Attribute Group
<Properties> the partition accepts the <group> element, which allows us to organize related properties.
Sample Code:
<properties> ......
<group name="Font"> <addd name="Name" type="string" defaultValue="verdana" /> <addd name="SizeInPoints" type="int" defaultValue="8" />
</group>
</properties>
Interaction with pages
To enable or disable the user configuration file function, you only need to set the enabled attribute of the <profile> element of the web. config file. If the value is false, the page cannot use the profile attribute.
Processing of Anonymous Users
Although the user configuration file function is designed for verified users, it can also store configuration file data of anonymous users. To enable Anonymous Users, you must first enable anonymousidentif.
Sample Code:
<system.web>
...... <anonymousIdentification enabled="true" />
<profile>
<properties>
<add name="BackColor" type="string" allowAnonymous="true" />
<add name="ForeColor" type="string" />
</properties>
</profile>
</system.web>
The preceding example allows anonymous users to set the background color but not the foreground color.
Access to user configuration file attributes
Before the request is processed, ASP. NET dynamically creates a class based on the user configuration file information defined in the web. config file. The profile attribute of the page is set to the instance of this class.
Personalized events
Before a request enters the processing cycle, the personalized data will be added to the HTTP context of a request. But which system component is responsible for loading the personalized data? Therefore, ASP. NET introduces a new module profilemodule.
This module has a pair of events that are triggered after the request is initiated and when the request is about to end. If the personalization function is disabled, the module will return immediately. Otherwise, it will trigger a personalize event to the application and load the personalized data in the current user configuration file. When a personalize event is triggered, personalized data is not loaded yet. The event handler triggered by the HTTP module must be written in global. asax.
Void profile_personalize (Object sender, profileeventargs E)
{
Profilecommon profile = NULL
// If the current user is an anonymous user, exit
If (user = NULL) return;
// Administrator user
If (user. isinrole ("Administrators "))
Profile = (profilecommon) profilebase. Create ("Administrator ");
Else
Profile = (profilecommon) profilebase. Create ("user ");
If (profile! = NULL)
E. Profile = profile;
}
Migration of anonymous user data
By assigning a unique ID to an anonymous user, the anonymous user can also store and obtain configuration information. However, if the anonymous user decides to create an account on the website at a certain time point, the configuration information set when the user is anonymous needs to be migrated to the new account.
When a user logs on to the real-name account anonymously, the personalized module triggers the migrateanonymous event. Properly Handle this global event and import anonymous user settings to the authenticated user's configuration file.
Sample Code:
Void profile_migrateanonymous (Object sender, profilemigrateeventargs E)
{
// Obtain the anonymous user configuration file
Profilecommon anonprofile;
Anonprofile = profile. getprofile (E. anonymousid );
// Migrate the anonymous user configuration file to the real-name User Account
Profile. backcolor = anonprofile. backcolor;
......
}
User Configuration File provider
The user configuration file consists of the access layer and storage layer.
The access layer provides a strong type model for obtaining and setting attribute values and managing user identities. It will ensure that the data obtained and stored is targeted at the currently logged-on users.
Storage Guide uses special providers to perform data-related tasks.
Configuration of the user configuration file provider
The default provider is specified by the defaultprovider attribute of the functional area described in the web. config file. By default, if no provider is specified, the first item in the Assembly is provided using a specific function by default.
The default configuration file provider is aspnetsqlprofileprovider, which uses SQL Server 2005 express as the data storage. As follows:
<profile>
<providers>
<add name="AspNetSqlProfileProvider"
ConnectionStringName="LocalSqlServer"
applicationName="/"
type="System.Web.Profile.SqlProfileProvider" />
</providers>
</profile>
The above code is taken from the machine. config file. Localsqlserver is not a connection string to a local or remote SQL server instance, but a data item name in the <connectionstrings> section of the configuration file.
Custom User Configuration File provider
We can write a Custom User Configuration File provider, but the Custom User Configuration File provider class should inherit from the profileprovider class.