Yeah, I’ve Done That: ASP.NET Profiles

Environment

  • ASP.NET 2.0

What

ASP.NET profiles are used to store user information in a database.

HttpModule in the pipeline: the ProfileModule which creates the profile object during the AcquireRequestState event of the request processing life-cycle.

When a web application is started, ASP.NET dynamically generates source code to create a class called ProfileCommon. The properties defined on this class are found in the section of the web.config file. These properties can be defined either in a element within the element or they can be defined by creating a class and referencing that class via the inherits attribute of the element.

Describing the properties within the element is a bad idea. This localizes the definition of ProfileCommon to your web app. and does not allow you to use this definition in other projects like web services. Sure, if your website is small and you don’t think in the future that web services or other projects will need to consume this information then define these properties in the element; but thinking like that will surely come back to bite you.

Note: If you had created your website as a Visual Studio Web Site project and then converted it to a Visual Studio ASP.NET Web Application project (as all good website projects should be) then the build mechanism for a web app. will not generate the ProfileCommon class for you. This means that all those custom properties you defined when the app. was a Web Site project are no longer available (generated) since the project is now a regular ASP.NET web app. project. For an ASP.NET web app. project you are forced to create a separate class to hold these properties. As luck would have it, this is a Good Thing.

Far better is to create a class—I like to call it UserProfile—that has the properties defined within it. Then point the ASP.NET profile code to this class via the inherits attribute on the element:

<profile enabled="true" inherits="<your-namespace>.UserProfile">

This causes ASP.NET, during app. startup, to create a class called ProfileCommon which inherits from UserProfile. Your UserProfile class itself is derived from ProfileBase. During the AcquireRequestState processing, ASP.NET creates an instance of the ProfileCommon class and makes it available to your code via the Profile property of the page’s HttpContext object.

Here’s an example UserProfile class for you VBers:

Public Class UserProfile
    Inherits System.Web.Profile.ProfileBase

    Public Shared Function GetCurrentProfile() As UserProfile
        Dim userName As String = Membership.GetUser().UserName
        GetCurrentProfile = GetUserProfile(userName)
    End Function

    Public Shared Function GetUserProfile(ByVal userName As String) As UserProfile
        Dim profile As UserProfile = Create(userName)
        GetUserProfile = profile
    End Function

    Const CID_NAME As String = "CID"
    Const PREFERRED_LANGUAGE_NAME As String = "PreferredLanguage"
    Const DEFAULT_HOME_PAGE_NAME As String = "DefaultHomePage"

    <SettingsAllowAnonymous(False)> _
    Public Property CID() As String
        Get
            Return GetPropertyValue(CID_NAME)
        End Get
        Set(ByVal value As String)
            SetPropertyValue(CID_NAME, value)
        End Set
    End Property

    <SettingsAllowAnonymous(False)> _
    Public Property DefaultHomePage() As String
        Get
            Return GetPropertyValue(DEFAULT_HOME_PAGE_NAME)
        End Get
        Set(ByVal value As String)
            SetPropertyValue(DEFAULT_HOME_PAGE_NAME, value)
        End Set
    End Property

    <SettingsAllowAnonymous(False)> _
    Public Property PreferredLanguage() As String
        Get
            Return GetPropertyValue(PREFERRED_LANGUAGE_NAME)
        End Get
        Set(ByVal value As String)
            SetPropertyValue(PREFERRED_LANGUAGE_NAME, value)
        End Set
    End Property

End Class
Using this class you can now get at the name of the user’s preferred language by referencing the Profile.PreferredLanguageName property in a Web form’s code-behind file.

References

  1. Essential ASP.NET 2.0 by Fritz Onion.
  2. Writing a Custom ASP.NET Profile Class by Jon Galloway.
  3. Profiles In ASP.NET 2.0 by Scott Allen.
  4. Examining ASP.NET's Membership, Roles, and Profile by Scott Mitchell.
  5. System.Web.Profile @ MSDN.

Feedback

ċ
UserProfile.vb.txt
(1k)
Glenn Doten,
Nov 21, 2009, 8:10 AM
Comments