Website Profile Object in Asp .net 2.0
CoderSource.net
Website Profile Object in Asp .net 2.0 - Article by azamsharp
Level: BeginnerType: Article
Rating: Page: 1 of 2

Date: 1/13/2006 12:00:00 AM

Environment: Windows, .Net, IIS, Visual Studio .Net 2005

 

Website Profile Object in Asp .net 2.0

Introduction:

Most of the websites provide the functionality which uniquely identifies the users.

This identification can be done using many methods. ASP.NET 1.X introduced Session objects which were used to maintain states of different users. In ASP.NET 2.0 we will see a new Profile class that eases the task for maintaining user's session.

Setting up the Website Profile Object:

The first thing you need to do is to set up the Profile object. You must remember that you simply cannot use the Profile Object like the code below:

Profile["UserName"] = "azamsharp";

By default, Asp .net Website Profile is saved in the SQL SERVER 2005 Database and hence it looks under App_Data to find the database. If however you would like to use the Profile object using SQL SERVER 2000 you can easily do so by running a very simple command line tool.

Simply go to your Visual Studio.NET 2005 command prompt and type aspnet_regsql.exe which will open a wizard. The simple wizard will guide you through various steps. The wizard will also ask you to select the database name on which you want to use the profile object. Many tables and stored procedures will be created in your selected database.

Take a look at the screen shot below which shows various new tables created:

Our table of interest is aspnet_Roles which will save the roles for the users.

Configuring Web.config for Profile Settings:

After installing the required databases you need to make some configuration settings in Web.config file. The first thing you need to do is to define the connection string for your database. Take a look at the code below which defines the connection string for the database.

<!-- CONNECTION STRING-->

<connectionStrings>

<add name="ConnectionString" connectionString="Server=localhost;Database=MyDatabase;Trusted_Connection=true"/>

</connectionStrings>

 

Now, comes the settings for the Profile Object. Take a look at the code below where I have defined the settings for the Profile object.

<anonymousIdentification enabled="true"/>

<profile defaultProvider="MyProfileProvider">

<providers>

<add name="MyProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ConnectionString"/>

</providers>

<properties>

<add name="Name" allowAnonymous="true" defaultValue="??"/>

<add name="PicturePath" allowAnonymous ="true" defaultValue="??"/>

</properties>

</profile>

Also, set the authentication mode to forms instead of Windows. Please note that Profile Object work with both Windows authentication as well as Form authentication. In this article I will be using Forms authentication.

Explanation of the code:

The first tag that you see the <anonymousIdentification enabled = "true" /> this means that the Website Profile will be created for anonymous users. This is a very useful feature if you are working with shopping carts. You can let the user do all the shopping using the anonymous profile and once they are ready to check out you can tell them that need to sign up to get their real profile. The <profile> tag has an attribute defaultProvider which indicates the name of the default provider. In this case we are using System.Web.Profile.SqlProfileProvider as the default provider. The other important attribute is the connectionStringName which represents the name of the connection string which we have already defined above.

The properties tags define the data and type of the data which you want to store in the Profile object. In this case I am storing the name and the PicturePath of the user. AllowAnonymous is set to true which means that the these properties can be set for anonymous users.

1 2

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us