Monday, September 5, 2011

Working with User Profile (.NET 3.5)



A user profile in ASP.NET is a set of properties that you define on a per-user basis for your site.

User profile basics
Configure a user profile provider
The profile provider class is used to store and retrieve user profile information to from a database.  There is default provider for Microsoft SQL Server.
Define the user profile
Set up the fields that you want to track for a user profile by using the Web.config file.
Uniquely identify users
You can identify both anonymous and authenticated users of your site. You use a unique value to return user’s profile from the data storage.
Set and save a user profile
You must provide a means that allows users to set their information. This information will be then saved by configured profile provider.
Recognize a returning user
When user returns to your site, you can retrieve his or her profile information as a strongly typed class.  Your code can then use the profile information to set customizations, prefill data entry fields and make other decisions related to the application.

These steps represent the basic elements you need to set up to use the ASP.NET Profile features.

Configuring a user profile provider:
                You store and retrieve user profiles in a database by using a provider class. ASP.NET provides a default configured provider for use with user profiles. This provider is the SqlProfileProvider class found in the System.Web.Profile namespace.
When ASP.NET is installed, a setting is added to the Machine.config file that connects the SqlProfileProvider class to an instance of a Microsoft SQL Server database on the local machine. This setting is AspNetSqlProfileProvider. The following example shown an example of the providers configuration.
<profile>
                <providers>
                                <add name=”AspNetSqlProfileProvider” connectionStringName=”LocalSqlServer”
  applicationName=”/”  type=”System.Web.Profile.SqlProfileProvider, System.Web,   
  Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d503a”/>
                </providers>
</profile>
In above markup the connection string is LocalSqlServer. This connection string is also found in Machine.config. By default it points to a SQL Server Express Edition version of a database named aspnetdb as below.
<connectionStrings>
<add name=”LocalSqlServer” connectionString=”data source=.\SQLEXPRESS; Integrated Security=SSPI; AttachDBFile=|DataDirectory|aspnetdb.mdf; User Instance=true” providerName=”System.Data.SqlClient”/>
</connectionStrings>

Aspnet_regsql.exe is used to configure new profile database.

Defining the user profile:
You can define a user profile by determining the individual fields you want to track for each user in your site. For example you might want to track first name, last name, the date and time of his or her last visited, preferred font, color settings etc. You define user profile fields inside the Web.config by adding a <Profile> element to the configuration file followed by a <properties> element.  Following is the example of user profile defined in a Web.config file.

<configuration>
                <system.web>
                                <profile>
                                                <properties>
                                                                <add name=”FirstName”/>
                                                                <add name =”LastName”/>s
                                                                <add name=”LastVisit” type=”Sysem.DateTime”/>
                                                </properties>
                                </profile>
                </system.web>
</configuration>

Anonymous User Profiles
By default, a user profile and its properties are enabled only for authenticated users – who provide logon credentials for your site. However you might want to allow anonymous user to use features of a user profile. To do so you define properties as anonymous by using the allowAnonymous attribute and must add the <anonymousIdentification> element to your web.config file as below.  

<anonymousIdentification enabled=”True”>

<profile>
<properties>
                                <add name=”FirstName” allowAnonymous=”True”/>
                                <add name =”LastName” allowAnonymous=”True”/>
                                <add name=”LastVisit” type=”Sysem.DateTime” allowAnonymous=”True”/>
                </properties>
</profile>

In above case anonymous profiles are enabled and ASP.NET creates a unique identification for each user the first time he or she visits your site. This value is stored and tracked with browsers cookie. By default this cookie is set to expire 70 days after the user’s most recent site access. If browser does not supports cookie, user profile can also function without them by storing unique identifiers in the URL of page request. However the profile is lost when the user closes his or her browser.


Profile Property Groups
You can group profile properties together under group name. For example you might want to define an address group that contains Street, City, PostalCode properties.
Web.config file will looks as below:
<profile enabled=”true”>
                <properties>
                                <group name=”Address”>
                                                <add name=”Street”/>
                                                <add name=”City”/>
                                                <add name=”PostalCode”/>
                                </group>
                </properties>
</profile>

Custom Profile Property Types
You can create your own custom class and use it as a profile property. To do so you need to mark your class as serializable by using Serializable attribute.
For example:
<profile>
                <properties>
                                <add name=”Position” type=”MyNamespace.OrgPosition” serializesAs=”Binary”/>
                </properties>
</profile>