Website Profile Object in Asp .net 2.0
 

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

Close    To Top
  • Prev Article-Programming:
  • Next Article-Programming:
  • Now: Tutorial for Web and Software Design > Programming > dotnet > Programming Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction