An Extended Look at the Profile Object - Part 1
page 3 of 5
by Bilal Haidar
Feedback
Average Rating: 
Views (Total / Last 10 Days): 34867/ 67

Using Simple Profile Properties

In this section, we will start by implementing a user profile that comprises a few simple properties. Listing 1 shows a web.config file with a sample profile configuration section. Note that only one profile configuration section can be defined in a web application. We cannot define another profile section in an application subfolder.

Listing 1: Simple Profile Property Configuration

<configuration>
    <system.web>
        <authentication mode="forms" />
        
        <anonymousIdentification enabled="true" />
        
        <profile enabled="true">
            <properties>
                <add name="FirstName" defaultValue="Bilal" type="string" 
                     allowAnonymous="true" />
                <add name="LastName" defaultValue="Haidar" type="string" 
                     allowAnonymous="true" />
            </properties>
        </profile>
    </system.web>
</configuration>

In this sample web.config file, Forms Authentication and Anonymous Identification are both enabled, thus allowing anonymous Profiles in the web application.

The profile configuration section is enabled and consists of the following custom properties:

  • FirstName: Representing the first name of the user visiting the web application. Its default value is set to Bilal and it is enabled for anonymous users.
  • LastName: Representing the last name of the user visiting the web application. Its default value is Haidar and it too is enabled for anonymous users.

Both profile properties have a data type of string. We could omit that parameter, since the default data type for any profile property is string.

The anonymousIdentification section, when enabled, generates a unique ID for each anonymous visitor. Enabling this feature makes the Profile object available for both anonymous and authenticated users. 

When the Profile section is defined in the web.config file, a Profile class is dynamically generated when the web application starts. This class, which inherits from the ProfileBase class, has strongly typed accessors added for each property defined in the profile section of the configuration file. This class is stored in the Temporary ASP.NET Files directory, which is the same place where the class files are stored for dynamically generated ASP.NET pages. Once the Profile class is generated, an instance is added to the current HttpContext object and populated with either the default values (for a new user) or retrieved values (for a returning user).

Once the Profile configuration section is defined, we can start accessing the Profile object from the server-side code as shown in the code below:

Profile.FirstName = "Johny";

The page in Listing 2 shows how to use a Profile object to persist user-specific information. This page displays a form to modify the values of the user's profile properties, which are FirstName and LastName. Then, after modifying the properties, it will display them above the form (see Figure 1).

Figure 1. Simple Profile Properties

 

Listing 2. SimpleProperties.aspx

<%@ Page Language="C#" %>
<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FirstNameTextbox.Text = Profile.FirstName;
            LastNameTextbox.Text = Profile.LastName;
        }
    }
    protected void Modify_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(FirstNameTextbox.Text))
            Profile.FirstName = FirstNameTextbox.Text;
        if (!string.IsNullOrEmpty(LastNameTextbox.Text))
            Profile.LastName = LastNameTextbox.Text;
    }
    protected void Page_PreRender(object sender, EventArgs e)
    {
        FirstNameLabel.Text = Profile.FirstName;
        LastNameLabel.Text = Profile.LastName;
    }

</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Simple Profile Properties</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>
            Simple Profile Properties</h1>
        <h3>
            Current Properties</h3>
        <p>
            First Name : <asp:Label ID="FirstNameLabel" runat="server" /><br />
            Last Name : <asp:Label ID="LastNameLabel" runat="server" /><br />
            </p>
        <hr />
        <h3>
            Modify Properties</h3>
        <p>
            First Name : <asp:TextBox ID="FirstNameTextbox" runat="server" /><br />
            Last Name : <asp:TextBox ID="LastNameTextbox" runat="server" /><br />
            </p>
        <p>
            <asp:Button ID="Modify" OnClick="Modify_Click" Text="Modify Profile"
                runat="server" />
            </p>
    </div>
    </form>
</body>
</html>

The SimpleProperties page in Listing 2 shows the Profile object in action. Notice that in Listing 1 we have enabled anonymousIdentification. We have also set allowAnonymous to true for both Profile properties. This allows us to access those properties even when the visitor to the web page is an anonymous user, which is the case when testing the page above.

Notice that if the browser is closed and then the same page opened again, the last modified values are displayed. This shows that the Profile properties were persisted, even for an anonymous user.

Finally, if we set allowAnonymous to false for each property, and try to set a profile property while the current visitor is an anonymous user, an exception would be thrown saying that one cannot access a Profile property that is not configured to be used by anonymous users.


View Entire Article

User Comments

Title: HELP   
Name: Ahamd
Date: 2010-03-29 8:22:16 AM
Comment:
Hi, I used this tutorial, but i get an error msg as, "Unable to connect to SQL Server database." I do not hav a SQL instance in my system. I'll hav to connect to a server in a network PC. So plz tell me what modifications i'll hav to do to run the code.
Title: Thank you   
Name: blad3runn69
Date: 2009-01-14 10:30:07 PM
Comment:
Thank you for the info and code Bilal, very helpful and muchly appreciated. :)
Title: An Extended Look at the Profile Object - Part 1   
Name: swapnil
Date: 2008-08-18 10:00:09 AM
Comment:
good one.
Title: Same Topic, Different Framework   
Name: RichardW
Date: 2008-04-25 5:39:37 PM
Comment:
Thank you. I always seem to get stuck on context. This got me past the context issues of Profiles in ASP.NET Membership.

Wondering if anyone in house has thought to go in depth on this same topic of Membership/Profiles but from the context of Ajax and Silverlight as the framework using ASP.NET 3.5 in the background. At the Olympics this year, NBC will be streaming with Silverlight... It's gonna be a boost for the technology, would like to come up to speed with others on it using the foundations of ASP.NET 3.5/Ajax to control it.
Title: Still don't see the point   
Name: Craig
Date: 2008-03-27 9:42:08 PM
Comment:
Good article but I really don't see the point of the profile object. If you're going to register users on a web site you'll have built that in to your DB schema and be authenticating them against that. Therefore, no point in having some bot do it for you.

If you're an anonymous user, how on earth can a system know who you are when you return to pick out your profile data again? I bet it's only available if you return within the session timeout period. In which case, use sessions, it's MUCH easier. It's becoming like you need a degree in computer science just to write a simple web page with .NET.
Title: Profiles with Web Applications   
Name: Mark
Date: 2007-06-11 9:44:59 AM
Comment:
Did you ever notice that if you use a web application instead of a website, the profile object is not available? Instead, you can access the currently logged on user profile through context.current.profile. The problem is, if you want to change information during the registration process, you can't do it. Do you have any way to change info in the profile of a user who isn't logged on using a web application?
Title: Thanks   
Name: john
Date: 2007-06-11 8:23:03 AM
Comment:
This helped me a lot to understand profile object.
Title: Fantastic Inroduction Article   
Name: |__Roshan__|
Date: 2007-02-09 4:04:06 AM
Comment:
This a fantastic intoduction article of the new ASP.NET personalization feature, the Profile object..
Title: Good Stuff!   
Name: Adi
Date: 2006-07-02 2:37:24 AM
Comment:
Nice intro! Easy and enjoyable to follow especially for .net newbie like me :)
Thanks Bilal!
Title: Re: All   
Name: Bilal Haidar [MVP]
Date: 2006-03-16 5:23:08 AM
Comment:
I am very glad you are happy with this article.

Thank you all.
Title: Good   
Name: Lajpathrai.Y
Date: 2006-03-16 4:43:37 AM
Comment:
Thanks a lot to author. really a good explanation. will useful to freshers
Title: Great Intro   
Name: Ed
Date: 2006-01-18 8:38:59 PM
Comment:
Perfect extension of the MS explanation (which was lacking a bit).
Title: Comment   
Name: Mihir Solanki
Date: 2005-12-24 9:17:06 PM
Comment:
Thanks, Perfect introduction
Title: Comment   
Name: Rainer
Date: 2005-11-17 10:31:58 AM
Comment:
Fine. But I am looking now for an easy way to work with these additional user profile properties. Like: Show me all users with zip code x.
Title: Nice article   
Name: Matthew Lee
Date: 2005-11-08 4:19:40 AM
Comment:
Nice, Informative stuff...






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-04-16 9:49:36 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search