ASP.NET profile

Profile: -ASP.NET profile service allows us to maintain the user
specific information.

User specific information includes favorite URLS, background
colors, themes, user settings and etc.

Provider services offer varios API’s to access and manage the user information from database.

How to set profile service:-

To activate the profile service, we need to specify “Profile” and “Provider” tag in configuration file. These tags will create classes at runtime and will perform task like storing and retrieving the user information from database.

1. Add <profile> element with a <provider> element defining the provider used to store and retrieve user-specific data.

<profile>
<providers>
<add name=”AspNetSqlProfileProvider”
connectionStringName=”LocalSqlServer”
applicationName=”/”
type=”System.Web.Profile.SqlProfileProvider,
System.Web, Version=2.0.0.0,Culture=neutral,

PublicKeyToken=b03f5f7f11d50a3a”/>

</providers>
</profile>

It is not mandatory to set provider detail. ASP.NET profile service will automatically create SQL express database and will store the property. But If want to use customize provider then we have to specifying configuration details.

2. Add a <properties> element and an <add> element for each user-specific data item that need to be included in the profile.

<profile>
<properties>
<add name=”FirstName”/>
<add name=”LastName”/>
<add name=”Age”/>
<add name=”City”/>
</properties>
</profile>

We can define the list of properties inside <properties> tag configuration file that we need to maintain.

Providers:-

ASP.NET 2.0 has default provider to handle the storage and retrieval of the user profile data.

The SqlProfileProvider supports storing the user profile information in SQL Express, SQL Server 7.0, SQL Server 2000, or SQL Server 2005 without requiring you to write any code.

All that is required is to add <providers> element and <profile> element in web.config.

Provider service will store all profile related information in aspnet_Profile table of ASPNETDB.MDF database.

SqlProfileProvider attributes

Name: – Defines the name for the provider.It can be any relevant value

Type: – Specifies the type of the property. Use the “class, assembly” notation in case of user-defined System.Web.Profile.SqlProfileProvider for the SqlProfileProvider.

ConnectionStringName: – Specifies the name of the connection string used to access the SQL database.

applicationName:- Specifies the name of the application using the profile provider. The default value is the value of the system.Web.HttpRequest.ApplicationPath property.

(Application name is value specified for the applicationNameattribute in the configuration file for the Webapplication; if it is not set we can set ApplicationVirtualPath value is used for ASP.NET applications.)

Properties attribute:-

Name: – This attribute specifies the name of the property in the dynamically created Profile class. The name property must be unique.

Type:-This attribute specifies the data type of property. The type can be any .NET data type or any custom data type. But data type must be serialized.

SerializeAs: – This property describes how data is serialized & storage in the database. Valid values are String, Xml, Binary, and ProviderSpecific

AllowAnonymous :– This attributes specifies if the property should be allowed to access anonymous users. Valid values are true and false ,The default value is false.

Provider:-This attribute specifies the profile provider to be used to persist the property data. We can specify different provides to persist the data of individual properties.

defaultValue:-Specifies the default value used when no profile data is available for the property. This value is not stored in the data store by default. It is used to initialize the property in the Profile object only.

readOnly:- Specifies if the property should be read-only. Valid values are True and false. The default value is false.

Profile page operation:-
We can directly access the defined properties through the ASP.NET profile object. ASP.NET profile service stores the profile object into database and makes it available after every page request.

Read values:- We can access the profile object property easily
e.g.:-
Label1.Text= “Profile stored successfully!<br/>” +
“<br/>First Name: ” + Profile.FirstName +
“<br/>Last Name: ” + Profile.LastName +
“<br/>Age: ” + Profile.Age +
“<br/>City: ” + Profile.City;

Write values:- By setting profile properties values will be automatically maintained over page round trips.

Profile.FirstName = TextBox1.Text;
Profile.LastName = TextBox2.Text;
Profile.Age = TextBox3.Text;
Profile.City = TextBox4.Text

Groupings: – We can create a hierarchy of profile properties using the group
element. Grouping allows us to categorize properties.
profile.Name= “scott”;
Profile.UI.Theme = “Reddish”;
Profile.UI.MasterPage= “layout.master”;

Configuration for group Property:-
<properties>
<add name=”Name”/>
<add name=”age”/>
<group name=”AutomobilePreferences”>
<add name=”CarModels” type=”System.Collections.ArrayList”/>
<add name=”PricePoint” type=”int” defaultValue=”18500″/>
</group>
</properties>

Custom profile classes: –

We can create our own class to handle the profile data instead of configuring the property in provider tag. This custom class has three specific requirements.

1. It must be inherited from profileBase.

2. Class must be serializable

3. The class must include a property for each user profile data item to be stored

At same time we have to set <profile> elements inherits attributes with the fully qualified namespace of custom profile class.

Example of Custom Class:-

1. Custom class definition example:-
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Profile;
public class UserDefinedProfileClass : ProfileBase
{
public UserDefinedProfileClass() {}
public List<String> JunkFood
{

get { return (list<String>)base[“JunkFood”]; }

set { base[“JunkFood”] =value; }
}
}

2. Setting inherit attributes of profile tag in configuration file

<profile enabled=”true” defaultProvider=”MyProfileSqlProvider” inherits=”UserDefinedProfileClass”>
<providers>

<add name=”MyProfileSqlProvider” type=”System.Web.Profile.SqlProfileProvider,
System.Web, Version=2.0.3600.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a” connectionStringName=”ASPNETDB” applicationName=”ProfileQuickStart”/>
</providers>

</profile>

Advantage of custom class:-
1.) We can use the same user profile data in multiple applications.
2.) It save the maintenance problem

Anonymous user profile – By default authenticated user is only eligible
to access the profile properties. However ASP.NET profile supports anonymous
users by configuring below setting

1.Add an <anonymousIdentification> element to enable the anonymous identification features and to define the cookie used to identify the user.

2. Set the allowAnonymous attribute of each profile property that is to be persisted for anonymous users to true.

When anonymous identification is enabled ASP.NET 2.0 generates
the GUID and use that ID for identification for all visitors to the application.

On every first visit GUID is generated and stored in cookies and that is persisted on
user’s computer.

For all subsequent requests, the cookies are retrieved and GUID is used to access his/her profile information.

<anonymousIdentification enabled=”true”
cookieName=”MyAppCookies”
cookiePath=”/”
cookieRequireSSL=”false”
cookieTimeout=”525600″
cookieSlidingExpiration=”true”
cookieProtection=”All”

/>
<profile enabled=”true” defaultProvider=” MyqlRoleProvider” automaticSaveEnabled=”false” >
<providers>

<add name=”MyqlRoleProvider” type=”System.Web.Security.SqlRoleProvider” connectionStringName=”sqlConnectionString” applicationName=”” />

</providers>
<properties>
<add name=”FirstName”type=”System.String”
serializeAs=”String”
allowAnonymous=”true”
provider=”MyqlRoleProvider”
defaultValue=””
readOnly=”false” />
<add name=”LastName”
type=”System.String”
serializeAs=”String”
allowAnonymous=”true”
provider=”MyqlRoleProvider ”
defaultValue=””
readOnly=”false” />
</properties>
</profile>

anonymousIdentification element attributes

enabled:- It indicates the HttpAnonymousIdentificationModule is enable the default value is true.

cookieName:- Name of cookies used to store the anonymous ID of user. The default is .ASPXANONYMOUS.

cookiesPath:- Defines the path to directory where cookies are stored. Unless your application requires specifying the path otherwise leave the path as “/”.

cookiesRequiresSSL:- Define whether an SSL connection is required to transmit the cookies to client. The default value is false.

cookiesTimeout:- Specifies the cookie expiration time in minutes.

cookiesSlidingExpiration:- Defines if sliding expiration of the cookie is enabled. If set to True, the cookie time to expire will be refreshed on every page request. If set to false,the cookie will expire at the time set when the cookie was created. The default value is true

cookiesProtection:- Defines the protection method used for the
cookie. Possible values are All, None, Encryption, and Validation. Validation specifies that the cookie data will be validated to ensure it was not altered in transit. Encryption specifies that the cookie is encrypted

Leave a comment