Web has evolved from the days of static web sites to modern day’s flashy, commercial and interactive sites. It is a common practice to tailor the web page according to your user’s individual taste or information. Personalization allows your site to remember your user and his/her details and present an individualistic environment. Previously you needed cookies to store user profiles and wrote codes to store user’s preference. ASP.Net provides services for personalizing a web site to suit a particular client’s taste and preference.

Introduction

Web has evolved from the days of static web sites to modern day’s flashy, commercial and interactive sites. It is a common practice to tailor the web page according to your user’s individual taste or information. Personalization allows your site to remember your user and his/her details and present an individualistic environment. Previously you needed cookies to store user profiles and wrote codes to store user’s preference. ASP.Net provides services for personalizing a web site to suit a particular client’s taste and preference.

Understanding Profiles

The heart of personalization service is the User Profile. A User Profile defines what kind of information about the user your site needs. For example—name of the user, date of birth, number of visits etc. These personalization properties are defined in the web.config file of your site so that ASP.Net can read and use it. This job is done by the personalization providers.

Conceptually, the profiles feature is a lot like creating your own database component. When you use user profiles, ASP.Net handles retrieving and updating the user-specific data automatically by using a back-end data source mostly a database.

ASP.Net provides two personalization providers: a profile provider for custom user data and a personalization provider for web parts. The basic provider jobs are defined in an abstract class named PersonalizationProvider. These providers allow you to load and save personalization properties and manage their relationship. Personalization is basically implemented by the SqlPersonalizationProvider class, derived from the PersonalizationProvider class.

To apply personalization in your site, you first decide on the user details you want to track. This is called a profile schema. Profile schema is defined within the web.config as name/type parts.

Let us create a sample site, where we want our application to remember user details like name, address, date of birth etc.
Add a web configuration file to your application, which should be edited as follows:

<profile>
        <properties>
          <add name="Name" type ="String"/>
          <add name="Birthday" type ="System.DateTime"/>
          <group name="Address">
            <add name="Street"/>
            <add name="City"/>
            <add name="State"/>
            <add name="Zipcode"/>
          </group>
        </properties>
      </profile>
    </system.web>
</configuration>

A profile tag has been added to the web.config file.

Once, defined in the web.config file the profile may be used in the site through the Profile property found in the current HttpContext and also available via page.
Profile is accessed in the site, in the same way as the session state. But instead of being presented by name/value pairs accessed by an indexer, the profile object is synthesized by the compiler based on the schema defined in the web.config file.

Let us go back to our project. Add the input controls such that it looks like the following screen:

 

Update Page_load to display profile information if it is already there.

using System;
using System.Data;
using System.Configuration;
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.txtname.Text = pc.Name;
                this.txtaddr.Text = pc.Address.Street;
                this.txtcity.Text = pc.Address.City;
                this.txtstate.Text = pc.Address.State;
                this.txtzip.Text = pc.Address.Zipcode;
                this.Calendar1.SelectedDate = pc.Birthday;
            }
        }

    }

Write a handler for the Submit button, so that when clicked the user details are saved into the profile.
 
protected void btnsubmit_Click(object sender, EventArgs e)
    {
        ProfileCommon pc = this.Profile.GetProfile(Profile.UserName);
        if (pc != null)
        {
            pc.Name = this.txtname.Text;
            pc.Address.Street = this.txtaddr.Text;
            pc.Address.City = this.txtcity.Text;
            pc.Address.State = this.txtstate.Text;
            pc.Address.Zipcode = this.txtzip.Text;
            pc.Birthday = this.Calendar1.SelectedDate;
            pc.Save();
        }

    }

When you run the page, you need to enter details of the information. But next time you visit the page, as the same user the details are already loaded.

Anonymous Personalization

This example uses the user name as the key for locating the user. ASP.NET also support ‘anonymous’ personalization i.e., you can track users via a cookie. For this you need to set the anonymousidentification element to ‘true’ and specify the cookie parameters:

<anonymousIdentification enabled =”true”
cookieName=”.ASPXANONYMOUSUSER”
cookieTimeout=”120000”
cookiePath=”/”
cookieRequiresSSL=”false”
cookieSlidingExpiration=”true”
cookieprotection=”Encryption”
coolieless=”UseDeviceProfile”/>

Profile Performance:

The profile feature could result in extra round-trip to database for each request in read-write situation and at least one trip for reading profile data. This feature does not integrate with caching, so every request that uses profile data needs a database connection. Keeping all these in mind, profile works best when you have a small number of pages accessing profile data and you are storing a small amount of profile data. On the other hand, profiles hamper performance when you have large number of pages accessing the profile data and storing large amount of data. Profile model retrieves the entire block of profile data.

Profile Properties

In our example, we have used just two profile properties name and type. The <properties> tag allows you to define the profiles more specifically. Let us look into some more advanced properties which could be used in the <add> element.


name: The name of the property.
type: By default the type is string but it allows any fully qualified class name as data type
serializeAs: The format to use when serializing this value
readOnly: A read only profile value can not be changed, by default this property is false
defaultValue: A default value that will be used if the profile does not exist or does not have information
allowAnonymous: A Boolean value indicating whether this property can be used with the anonymous profiles
Provider: The profiles provider that should be used to manage just this property

Profile Serialization and its Limitation:

Profile serialization means how the profile data is stored by the profile provider. The default profiles provider included with the ASP.Net serializes profile information into a block of data inserted into a single field in a database record. For example, our example profile data will be stored using a format like:

Name:S:0:11:Street:S11:19:City:S:30:6:State:S:36:10

:ZipCode:S:46:5:Country:S:51:6

It indicates the name of the property, then a colon as delimiter, next character showing the type, next field showing from where to start and next field showing how many characters to read. This approach is alright for storing any kind of data but when it comes to using the profile data in some other application; you need to write a custom parser to read this profile data which is tedious to the least, and even then if it not possible to query this data. This problem has two solutions.

• Use custom data access components instead of profiles to store and retrieve data in a database as used to be done before advance of ASP.Net 2.0
• Create a custom profiles provider that’s designed to store information using your database schema.

Conclusion:

We have discussed in a nutshell what user profile is and how it could be used for personalization. We have also observed when maintaining a profile works well and when it does not. This kind of techniques is extremely helpful for designing shopping carts and other e-commerce oriented sites. This article tries to take you to the threshold of vast opportunities with the hope that you take up the journey towards more complexity and enjoy it thoroughly.

Happy Programming!

About the Author:

NAME: PIYALI SENGUPTA

QUALIFICATION: B.E., M.C.A

AREA OF INTEREST: WEB PROGRAMMING

HOBBIES: READING AND TRAVELLING

I HAVE MOSTLY WORKED AS A FACULTY. PRESENTLY WORKING FROM HOME.I LOVE MATHEMATICS, ALGORITHMS, PROGRAMMING, FRIENDS, CHILDREN AND NATURE NOT NECESSARILY IN THIS ORDER.YOU CAN CONTACT ME AT: itspiyali@gmail.com