Advertisment

Profiles in ASP.NET 2.0: Part I

author-image
CIOL Bureau
Updated On
New Update

This article focuses on the Profiles feature in ASP.NET 2.0. As you all know that a profile is a set of information which varies from user to user and is unique for each user. That means for each user a matching set of profile can be stored or retrieved whenever necessary without the need to input the data again and again.



Need for Profiles



Browsing in Websites has been the forte of each and every person and surely each person has his own style of browsing or collecting information from the Website. He would also like to customize the look and feel of the site as well as save the settings so that they could be retrieved and applied the next time he visited the site. Also there was a feature known as shopping online and that required the visitor to be registered on the site. That meant that a visitor had to be signed each and every time he performed shopping online. And in case he didn't want to purchase the items he would have to abort the process and sign out of the site. And this possessed a problem because if the visitor selected some items to be added to the shopping cart, the site would not allow him to do so because it was unable to identify the visitor so as to map his identity with the items purchased. And when he signed on the site, the items that he had added to the shopping cart would be lost because there was no way to copy the data from the anonymous credentials to the signed in credentials.



Coding had to be done by the developer of the Website to implement all these functionality and it was a tedious process to be implemented. Also when some changes was required to this functioning there was no way to implement the changes without modifying the other parts of the Website which was a nightmare to maintain.



What ASP .NET 2.0 brings?



ASP.NET 2.0 now solves this problem and provides a new user experience when navigating through Websites and this concept of preserving user data and preferences is known as "Profiles".



Profile properties in ASP.NET 2.0 allow users to associate information in their application with individual visitors to their site. Properties can be defined such as information that the user wants to track, personal information (address, city), preference information (preferred color scheme, list of stocks to track), or other user-specific information your application might need (shopping cart). When the application runs, ASP.NET 2.0 creates unique instances of the properties for each user, and then the user can assign values to those properties.



For example, on a home page of a Website, the user might allow the visitor to pick the name of a color scheme. Then, in code, the user's choice can be stored in a profile property. ASP.NET 2.0 associates the property value with the user and stores it in a database. When the visitor visits the site another time, the code can look up the profile property values. ASP.NET 2.0 looks up the property value for that visitor and makes it available to the application.



Profiles are unique from session variables in the way that they can be persisted across trips and from time to time whereas the session variables are persistent as long as the browser window is open or as long as the user is logged onto the side. Session variables are highly volatile i.e. they are susceptible to getting destroyed on session time out or on closure of the browser window. Also session variables are non-transferable from window to window.



A new feature of Profiles in ASP.NET 2.0 is that they now support anonymous users i.e. even if a visitor isn't logged into the Website it will be able to keep track of his settings and save it. And it can also export these settings from anonymous to identified settings when he next logs in to the Website.



We will examine how the "profile feature" can be implemented in Websites to persist unique user information. We will implement the features that we discussed earlier.



Let's create an application that will have the following features.



    Advertisment

  1. A Website that accepts the data from the visitor and then saves it in the profile of the anonymous visitor and then shows that the data is persistent even on exiting the Web application.


  2. A feature where the user can login to the Website and show that the data which was created as an anonymous user is visible even after logging on, thereby saving the need to input data again after logging on to the Website.
  3. We will be using Visual Studio .NET 2005 codenamed Whidbey beta 1. We will also be making use of the new feature called ASP.NET Configuration Control Panel, which allows managing security and access to the Website. Through out this demo we will refer to Visual Studio .NET 2005 Beta 1 as Whidbey.



    Open Whidbey and then click on New Website. You can create Websites either through Internet Information Server or from a flat file folder. Whidbey will do the necessary actions to launch its own Web server when the application is loaded. This is similar to PWS found in the days of Windows 95 and Windows 98. For sake of simplicity and also to follow the concept of "least privileges" we will be using the file system to create our Website.



    Click on File | New Website

    and then choose ASP.NET Website as the template and Visual Basic as the project type. Specify the location of the Website as a flat file path i.e. "C:\Demos\Profiles" and then click on 'OK.' Whidbey does the initiation of the process of creating the folder and then inserts the default pages for the Website. After a little while of activity the default page opens in code view.



    Advertisment

    Now before working on the page, we will configure ASP.NET to create the profile and also specify the data that will be captured as part of the profile. Note that the steps explained below are new in Whidbey. We will be using the ASP.NET Configuration Control Panel.



    Click on Website | ASP.NET Configuration, to start the Website administration tool.

    This allows to manage various aspects of the Website namely Security, Application, Provider and Profile.
    Advertisment


    Now we will work on the profile section and leave the rest for a later time. Click on the Profile tab and then do the following. If the settings in Enable profile are "Enable Profile" then click on it to enable the profile. Also check the checkbox to enable the profile feature for anonymous users.



    Advertisment

    Then we will create various profiles and also assign the datatype to each profile. This allows us to maintain the right type of data to each profile. Now click on Property Name and then enter the word Location. This profile will save the location of the user whenever he enters the name in the application. Let the default "data type" be String. Check the box that enables the profile for anonymous users. That is all that needs to be done for creating a profile value. Simple isn't it.



    For the sake of simplicity we will limit ourselves to creating only one profile. By default profile information is stored in an "Access database" in the Data folder of the Web directory. This is the default setting for any Website configuration, though other providers like SQL Server are also supported. For security and reliability it is recommended to use SQL Server for saving the profile settings. For this demo we will limit ourselves to use the Access provider.

    Advertisment


    Let us see what happens to the application and what goes on in the background when the provider is created. When the wizard creates the profile it is actually inserting the details in the Web.config file. Let us open the Web.config file and see its contents.



    Advertisment

    <

    profile>

    <

    properties>

    <

    add name="Location" type="System.String" allowAnonymous="true" />

    properties>



    profile>



    Now let us use this profile property in the Website and see how the data is saved and retrieved across user sessions. Create a new page called "Profile.aspx" in Whidbey. You can use the normal process to add new pages or you can use the default page if you like. There is no restriction to what page we are using for this presentation.



    Advertisment

    Drag and drop the following controls on the page and then lay the controls as follows.







































    Type of control



    Name



    Value/Caption



    Text box



    txtLocation



    Nil



    Button



    cmdSave



    Save



    Label



    lblLocation



    Nil

    Lay the textbox and button on the first line and the label below it.



    Now we will write code for the clicked event of the button. Write the code as below.



    Sub

    cmdSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)



    Profile.Location = txtLocation.Text



    lblLocation.Text = Profile.Location



    End

    Sub

    When profiles are enabled for the Website, ASP.NET 2.0 dynamically creates a property called "Profile" and then the individual properties are available through the Profile Property.



    Now we will have to write code for the Page Load event so that the page will be able to display the value of the profile when the page is submitted. It will also allow displaying the value when the site loads for that particular user.



    Put the following code in the Page Load event.



    Private

    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    lblLocation.Text = Profile.Location



    End

    Sub

    Now let us test this page and see if the profile value is saved for the location. Start the Website by clicking F5 or Ctrl+F5 (without debugging). Then when the page loads, enter the location in the text box and then click on Save. It is seen that the label is populated with the location that is just entered. Now close the browser window and then load the site again by either pressing F5 or opening a blank browser window and then entering the Website.



    You will see that the label displays the value of the location and this shows that the value is persisted. This is because the value is saved in the Access database and is mapped to a unique identity that identifies the anonymous user.



    To be continued....



    Watch out for the next part of this article, which talks about making use of custom data type for creating the location profile.



    Disclaimer



    "Since this article was written when the technology .NET 2.0 is still in development, there is no guarantee that the features explained will be there in the final version and is subject to change. This article should be taken only for getting a general idea of what is going to be available in .NET 2.0 and not the actual features that will be a part of the final release of .NET 2.0"



     



    **

    tech-news