Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Using ASP.Net Profiles
By Peter Aitken

Rate This Article: Add This Article To:

Using ASP.Net Profiles - ' The Default Profile Provider '
( Page 2 of 3 )

Many Web sites strive to give their users a personalized experience. The Web site "remembers" each user and provides personalization and/or convenience features that can make the site experience more pleasant. For example, a web portal can present a user-selected array of information, weather, and sport scores for Jack but shows stock prices and news for Jane. Another example is when a shopping site remembers your shipping address and shoe size.

The most common way this is done is with cookies: small bits of data that are stored on the user's system and retrieved when they visit the Web site again. With ASP.Net, you have another and, I think, better option in profiles.

Any means of providing user-specific features requires several things: the information must be persisted in a user-specific manner, returning users must be accurately recognized, and the related information must be retrieved. The beauty of profiles is not that they make possible something that was impossible before, but rather that they make it so easy.

Profiles are implemented using .Net's provider model and, as you might well guess, they make use of profile providers. You can use the default profile provider, which stores data in SQL Server and is adequate for the vast majority of needs, or you can implement your own custom profile provider.

The Default Profile Provider

The first step in using the default SqlProfileProvider is to create the database that it will use. You do this with the aspnet_regsql.exe command line utility using the -A p command line switch to specify the addition of the profile feature. (Of course, SQL Server must be available for this to work!) Then, you enable providers in your ASP.Net application by modifying the configuration file config.web: as in this example:

<profile defaultProvider="SqlProvider">
      <providers>
        <clear />
        <add name="SqlProvider"
          type="System.Web.Profile.SqlProfileProvider"
          connectionStringName="SqlServices"
          applicationName="My ASP.Net Application"
          description="SqlProfileProvider for My ASP.Net Application" />
      </providers>
</profile>

The profile must be customized by defining the properties you need. This too is done in the web.config file, placing a <properties> tag within the <profile> tag and enclosing within it the names of the desired properties:

<properties>
<add name="Address" /> <add name="City" /> <add name="State" /> <add name="ZIP" /> </properties>

When your application executes, .Net dynamically creates a ProfileCommon class that inherits from ProfileBase class and includes all of the properties you defined in the web.config file. In your application, this class is accessible through the Profile property of the current HttpContext. You save and retrieve profile data using the usual syntax. For example, if your page has a text box for entry of ZIP code, you would save the user-entered value in the profile like this:

Profile.ZIP = txtZIPCode.text

Or you would retrieve the value from the profile, like this:

txtZIPCode.text = Profile.ZIP

What makes this so easy is that you do not need to take any steps to identify the current user; the framework takes care of that for you.

By default, profile properties are stored as strings. You can also define a property as any available .Net class, such as Int32 or DateTime. For example:

<properties>
<add name="BirthDate" type="System.DateTime" /> ... </properties>

A profile property can be a custom classes that you have defined. The only restriction is that the class you defined must support serialization.

While profiles can be used for anonymous users as well as authenticated users, individual profile properties are, by default, managed only for the latter. If you want a property to be managed for your site's anonymous visitors, you must set the allowAnonymous attribute to True in the property definition:

<properties>
    <add name="Name" allowAnonymous="true" 
/>
    ...
</properties>



 
 
>>> More Using Microsoft Visual Studio Articles          >>> More By Peter Aitken