[ASP.NET Membership] User Profiles

The “User Profiles” is one type of state management in ASP.NET and defines per-user based information, even for anonymous users. The main difference is that profiles are designed to store information permanently (usually in databases). ASP.NET provide profiles as a whole package. You do not need to design DB tables or even do not need to write DB access code. Profiles are integrated with ASP.NET completely.  

1. Configuring profiles in the Web.config

  • The default provider is the “SqlProfileProvider“.
  • If you use SQL Server Express, the default database file is “ASPNETDB.mdf”.
<profile>
  <providers>
    <add name=”AspNetSqlProfileProvider” applicationName=”/”connectionStringName=”LocalSqlServer”  type=”System.Web.profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” />
  </ providers >
</ profile >

2. Configuring a new profile database

If you use the full version of SQL Server, you need to configure database for membership and profiles.

The “aspnet_regsql.exe” executable is used to setup the profile tables.

  • -d: database name
  • -S: server name
  • -E: Windows credentials
  • -U: login user name
  • -P: password
  • -A all: all membership tables
  • -Ap: only profile tables

> aspnet_regsql.exe -S (local) -E -A all

3. Defining Profile Properties

Profile Fields, or Properties, are defined in the Web.config. You can add fields in the <add> element within the <profile> and <properties> elements

– Attributes of <add> element

  • name: the name of the property
  • type: The fully qualified class name (the default is System.String)
  • defaultValue: a default value if the profile does not exist
  • readOnly: indicates whether the property can be changeable or not
  • allowAnonymous: indicates whether the property can be used by an anonymous user
  • serializeAs: the type of serialization (String, Xml, Binary, or ProviderSpecific)

Properties can be even grouped using the <group> element. The address can be a good example of a group of multiple properties such as street and city.

By default, profiles and their properties are only for authenticated users. Use the “allowAnonymous” attribute for anonymous users to use profiles. Also the “anonymousIdentification” element should be enabled too. (Cookies are used.)

<anonymousIdentification enabled=”true” />
<profile>
  <properties>
    <add name=”Name” allowAnonymous=”true”  />
    <add name=”LastVisit” type=”System.DateTime” allowAnonymous=”true” />
    <group name=”Address”>
      <add name=”Street” />
      <add name=”City” />
    </group>
  </ properties >
</ profile >

4. Using Custom Types for Profile Properties

Wouldn’t it be nice if you use custom class as a Profile property? Luckily, you can!!! With one condition: the class should be serializable.

  • Mark the class as serializable
  • Set the type attribute to the fully qualified mane of your custom type
[Serializable()]
public class Address { ... }
<profile>
<properties>
<add name=”Address” type=”MyCom.Address”  />
</ properties >
</ profile >

5. Using Profile Properties

You are ready to use the profile now. How can you access profile properties in your code?

ASP.NET creates a strongly typed class. In your web page, you can access the strongly-typed profile object through the “Profile” property.

You can access profile properties using the following syntax:

  • Profile.PropertyName
  • Profile.GroupName.PropertyName

Retrieving and Updating profiles to the database are automatically performed in the background by ASP.NET.

protected void Page_Load(object sender, EventArgs e)
{
  lblInfo.Text = "Name:  " + Profile.Name;
  lblInfo.Text += "<br /> Address:  " + Profile.Address.Street + ", " + Profile.Address.City;
}

If you need to access profiles from business classes, you can do this through the “HttpContext.Current.Profile” object. You can cast this object to the “ProfileCommon” object, which is strongly typed.

6. Accessing Profiles of the Specified User

You can access profiles of users other than the current user.

Simply use the “Profile.GetProfile(username)” method, which returns a “ProfileCommon” object. If you want to save the profile of other users, you need to call “Profile.Save()” method explicitly. It is not required when you modify the profile of the current user. In the case of the current user, Save() is called automatically when the page is unloaded.

7. Automatic Saves

If you include the complex types, ASP.NET does not detect the changes and saves the profile at the end of every request. This behavior ensures that profiles data are always up-to-date but adds unnecessary overhead. The solution is to disable automatic save.

<profile automaticSaveEnabled="false">

Now, you need to save profiles by calling the “Profile.Save()” method explicitly when the profile data are updated.

8. Migrating Anonymous User Profiles

What if previous anonymous user logs in? The user can setup the profile again but it might be better if the previous profile is transferred automatically.

You can do this by handling the “MigrateAnonymous” global event in the “Global.asax“.

  • Get the “ProfileCommon” object of the annoymous user by calling the “Profile.GetProfile()” method (ProfileMigrateEventArgs has an “AnnoymousID” property)
  • Copy information
  • Delete the annoymous Profile
  • Remove the annoymous identifier
void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe)
{
  ProfileCommon anonymousProfile = Profile.GetProfile(pe.AnnoymousID);
  if (!anonymousProfile.IsNullOrEmpty())
  {
    Profile.Address = anonymousProfile.Address; // copy data
  }
  System.Web.Profile.ProfileManager.DeleteProfile(pe.AnnoymousID);
  AnnoymousIdentificationModule.ClearAnnoymousIdentifier();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s