Techbubbles

ASP.NET Profiles overview

ASP.NET Profiles feature allows the users to personalize their content on the web site. Profiles can be used in different scenarios like when you visit the news web site or sports website then you can allow the user to choose their favourite categories and then you can store them in a profile to show the pages based on their preferences. Using ASP.NET Profiles you can keep the user details such as First Name, Last Name, Date of Birth and their interests. You can use this information to create a closer relation ship with your site users. This post explains the steps to configure and create a profile in ASP.NET.

Configuring ASP.NET Profile

ASP.NET Profile enables you to store the data for registered users and as well as for anonymous users. You need to define the information that you want to store in Web.config file. ASP.NET gives you the access of these properties on Profile object in all pages in your site. Create the profile properties in web.config as shown below

<system.web>
  <profile>
      <properties>
        <add name="FirstName" allowAnonymous="true"  />
        <add name="LastName" allowAnonymous="true" />
        <add name="DOB" type="System.DateTime" />
      </properties>
   </profile>
</system.web>

when you specify anonymous attribute for a property then it is available for logged in users and anonymous users otherwise it is only available for logged in users. default type for a property is string. The possible attribute values for add element are

image

Creating Profile Groups

Profile Groups can contain group of properties. You can logically group the properties by adding <group> element to the <properties> element of profile as shown below

    <profile>
      <properties>
        <add name="FirstName" allowAnonymous="true"   />
        <add name="LastName" allowAnonymous="true" />
        <add name="DOB" type="System.DateTime" />
      <group name="BusinessAddress">
        <add name="Street"/>
        <add name="PostCode"/>
        <add name="City"/>
      </group>
      </properties>
    </profile>

Adding Custom Data Type to Profile property

You can use the custom data type defined in your application by referring with a fully qualified name. The custom data type can look as below

namespace ProfileDemo
{
  public class Favourites
  {
      public string Sport {get; set; }
  }

}

refer the above data type in web.config as below

<add name="Favourites" type="ProfileDemo.Favourites">

You can access these properties in code behind file as below

    protected void Page_Load(object sender, EventArgs e)
    {
        Profile.FirstName = "James";
        Profile.LastName = "Bond";
        Profile.BusinessAddress.Street = "Stree1";
        Profile.BusinessAddress.PostCode = "007";
        Profile.BusinessAddress.City = "London";
    }

When you create profile properties and groups then ASP.NET runtime creates a class called ProfileCommon. The data will be saved in Profile table using asp.net profile provider.

Related Posts:

%d bloggers like this: