<configuration><system.web>
<profile>
<properties>
<add name="DisplayName">
<add name="ShowOnlineStatus" type="System.Boolean">
<group name="UserInterface">
<add name="PageTheme" defaultValue="Green">
<add name="EmailArticles" type="System.Boolean" defaultValue="false">
<group>
</properties>
</profile>
</system.web></configuration>
The green colored config section is specific to profile. Although the elements are self describing let me explain the elements and it's attributes. By adding these elements and attributes we are asking the IDE and ASP.Net to recognize these properties as part of the profile object exposed in our Page class.
So in the above scenario our profile object will have properties like DisplayName, ShowOnlineStatus which is of type boolean and also we are asking to group PageTheme and EmailArticles inside UserInterface. So the final profile object will have the below given structure
Page.Profile.DisplayName = "Sachin Joshi";
Page.Profile.ShowOnlineStatus = true;
Page.Profile.UserInterface.PageTheme = "Blue";
Page.Profile.UserInterface.EmailArticles = false;
But hold a second, how did our profile object in our page got to know about the configuration and exposed these strongly-typed properties. It's not magic but the ASP.Net parser have recognized the profile configuration and have generated a class called ProfileCommon inherited from ProfileBase and have exposed the configured properties. Just check your App_Code folder and you will find a class called ProfileCommon inherited from ProfileBase.
So far so good, we have created a very simple configuration for personalization and in our web pages we will give our users the ability to save these details. But the question here is where exactly these details are saved.
Actually we have not yet configured any profile provider which helps us in storage and retrieval of these customized profile. SqlProfileProvider comes to our rescue, this class is part of the ASP.Net 2.0 framework and as the name suggests it stores and retrieves profile data to and from Sql Server.
Below given is the sample configuration of profile provider.
<configuration><system.web>
<profile>
<providers defaultprovider="SqlServerProfileProvider">
<add name="SqlServerProfileProvider"
connectionStringName="SqlServer2005"
applicationName="/"
type="System.Web.Profile.SqlProfileProvider" />
</providers>
</profile>
</system.web></configuration>
In the above config we are asking ASP.Net to use the connection string from the specified section and use the SqlProfileProvider. So when we use the profile object ASP.Net in turn will be responsible to create an instance of the right provider and use the provider to store and retrieve the profile data.
Below is the final configuration we will end up with to acieve the objective.
<configuration><system.web>
<!-- Connection String START-->
<connectionStrings>
<add
name="SqlServer2005"
connectionString="server=SOME_SERVER;database=SOME_DB;uid=XXXXXX;pwd=XXXXXX;"
/>
<add
name="MYSql"
connectionString="server=SOME_SERVER;database=SOME_DB;uid=XXXXXX;pwd=XXXXXX;"
/>
</connectionStrings>
<!-- Connection String END-->
<profile>
<!-- Profile Properties START-->
<properties>
<add name="DisplayName">
<add name="ShowOnlineStatus" type="System.Boolean">
<group name="UserInterface">
<add name="PageTheme" defaultValue="Green">
<add name="EmailArticles" type="System.Boolean" defaultValue="false">
<group>
</properties>
<!-- Profile Properties END-->
<!-- Profile Providers START-->
<providers defaultprovider="SqlServerProfileProvider">
<add name="SqlServerProfileProvider"
connectionStringName="SqlServer2005"
applicationName="/"
type="System.Web.Profile.SqlProfileProvider" />
<add name="MySqlProfileProvider"
connectionStringName="MySql"
applicationName="/"
type="fuzzydev.Providers.MySqlProfileProvider" />
</providers>
<!-- Profile Providers END-->
</profile>
</system.web></configuration>
Note that in the above configuration there are two connection strings, one pointing to Sql Server and the other to MySql. Similarly there are two providers one for each DBMS, now we can switch to any provider by changing the defaultProvider attribute in the providers section.
Our UI need not worry about which provider or dbms is being used, this is made possible with combination of polymorphism, configuration and reflection.
Before you start playing with it there are couple of things which would be helpfull.
By default all the primitive types are serialized as string, this can be
overriden by specifying the serializeAs attribute in the configuration
<add name="DisplayName" serializeAs="Binary" />
or
<add name="DisplayName" serializeAs="Xml" />
- Allow Profiling For Anonymous Users
Profiling can also be done for anonymous users we just need to specify
allowAnonymous="true" in the configuration.
<add name="DisplayName" allowAnonymous="true" serializeAs="Xml" />
Anonymous users are tracked using cookies, this discussion will require a
whole new blog entry and so further details are deferred.
This is just a brief introduction of ASP.Net profiling, there are lot of things which needs to be looked in to like
- Writing our own custom profile class by inheriting from ProfileBase and setting the attributes like allowAnonymous, serializeAs to the properties.
- Writing our own profile provider for our custom data store for example Xml, Access, etc.
- Migration of saved preferrences of anonymous users when they are promoted to registered users.
ASP.Net profiling is a great way of adding richness to your website with the simplicity and flexibility of plug and play components.