Export (0) Print
Expand All

ProfileBase Class

Provides untyped access to profile property values and information.

Namespace:  System.Web.Profile
Assembly:  System.Web (in System.Web.dll)

'Declaration
Public Class ProfileBase _
	Inherits SettingsBase

The ProfileBase type exposes the following members.

  NameDescription
Public methodProfileBaseCreates an instance of the ProfileBase class.
Top

  NameDescription
Public propertyContextGets the associated settings context. (Inherited from SettingsBase.)
Public propertyIsAnonymousGets a value indicating whether the user profile is for an anonymous user.
Public propertyIsDirtyGets a value indicating whether any of the profile properties have been modified.
Public propertyIsSynchronizedGets a value indicating whether access to the object is synchronized (thread safe). (Inherited from SettingsBase.)
Public propertyItemGets or sets a profile property value indexed by the property name. (Overrides SettingsBase.Item.)
Public propertyLastActivityDateGets the most recent date and time that the profile was read or modified.
Public propertyLastUpdatedDateGets the most recent date and time that the profile was modified.
Public propertyStatic memberPropertiesGets a collection of SettingsProperty objects for each property in the profile.
Public propertyPropertyValuesGets a collection of settings property values. (Inherited from SettingsBase.)
Public propertyProvidersGets a collection of settings providers. (Inherited from SettingsBase.)
Public propertyUserNameGets the user name for the profile.
Top

  NameDescription
Public methodStatic memberCreate(String)Used by ASP.NET to create an instance of a profile for the specified user name.
Public methodStatic memberCreate(String, Boolean)Used by ASP.NET to create an instance of a profile for the specified user name. Takes a parameter indicating whether the user is authenticated or anonymous.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetProfileGroupGets a group of properties identified by a group name.
Public methodGetPropertyValueGets the value of a profile property.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitialize(String, Boolean)Initializes the profile property values and information for the current user.
Public methodInitialize(SettingsContext, SettingsPropertyCollection, SettingsProviderCollection)Initializes internal properties used by SettingsBase object. (Inherited from SettingsBase.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSaveUpdates the profile data source with changed profile property values. (Overrides SettingsBase.Save.)
Public methodSetPropertyValueSets the value of a profile property.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

ASP.NET uses the ProfileBase class to create the class used for the user profile. When an application that has the user profile enabled is started, ASP.NET creates a new class of type ProfileCommon, which inherits from the ProfileBase class. Strongly typed accessors are added to the ProfileCommon class for each property defined in the profile configuration section. The strongly typed accessors of the ProfileCommon class call the GetPropertyValue and SetPropertyValue methods of the ProfileBase base class to retrieve and set profile property values, respectively. An instance of the ProfileCommon class is set as the value of the Profile property for the ASP.NET application.

To create an instance of a user profile in an ASP.NET application, it is recommended that you use the Create method.

Notes to Inheritors

You can create a custom profile implementation that inherits from the ProfileBase abstract class and defines properties for the user profile that are not specified in the profile configuration element. You can specify a custom user-profile type in the Web.config file with the inherits attribute of the profile configuration element, as shown in the following example. The code for the EmployeeProfile class is included in the Example section of this topic.

<profile inherits="Samples.AspNet.Profile.EmployeeProfile"
  defaultProvider="SqlProvider">
  <providers>
    <clear />
    <add
      name="SqlProvider"
      type="System.Web.Profile.SqlProfileProvider" 
      connectionStringName="SqlServices" 
      description="SQL Profile Provider for Sample"/> 
    <add
      name="EmployeeInfoProvider"
      type="System.Web.Profile.SqlProfileProvider" 
      connectionStringName="SqlServices" 
      description="SQL Profile Provider for Employee Info"/> 
  </providers>

  <properties>
    <add name="GarmentSize" />
  </properties>
</profile>

The following code example shows a Web.config file that specifies a user profile that contains a ZipCode property of type string and a RecentSearchList property of type StringCollection. The generated Profile property of the current HttpContext will have strongly typed accessors for each of the specified properties.

<configuration>
  <system.web>
    <anonymousIdentification enabled="true" />

    <profile enabled="true" defaultProvider="SqlProvider" >
      <properties>
        <add name="ZipCode" allowAnonymous="true" />
        <add name="RecentSearchList"
          type="System.Collections.Specialized.StringCollection"
          serializeAs="Xml"
          allowAnonymous="true" />
      </properties>
    </profile>
  </system.web>
</configuration>

The following code example shows an ASP.NET page that reads and sets the ZipCode property specified for the user profile. Before attempting to run this code, set the provider to the default AspNetSqlProvider in the ASP.NET configuration settings for the Web site.

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Public Sub Page_PreRender()

  If Profile.ZipCode = Nothing Then
    PersonalizePanel.Visible = False
    GetZipCodePanel.Visible = True
  Else
    ZipCodeLabel.Text = Profile.ZipCode

    ' Get personalized information for zip code here.

    PersonalizePanel.Visible = True
    GetZipCodePanel.Visible = False
  End If

End Sub

Public Sub ChangeZipCode_OnClick(sender As Object, args As EventArgs)
  ZipCodeTextBox.Text = Profile.ZipCode
  Profile.ZipCode = Nothing

  PersonalizePanel.Visible = False
  GetZipCodePanel.Visible = True
End Sub

Public Sub EnterZipCode_OnClick(sender As Object, args As EventArgs)
  Profile.ZipCode = ZipCodeTextBox.Text
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Home Page</title>
</head>
<body>

<form id="form1" runat="server">
  <table border="1" cellpadding="2" cellspacing="2">
    <tr>
      <td>
        <asp:Panel id="PersonalizePanel" runat="Server" Visible="False">
          Information for Zip Code: <asp:Label id="ZipCodeLabel" Runat="Server" /><br />
          <!-- Information for Zip Code here. -->
          <br />
          <asp:LinkButton id="ChangeZipCodeButton" Runat="Server" Text="Change Your Zip Code"
                          OnClick="ChangeZipCode_OnClick" />
        </asp:Panel>
        <asp:Panel id="GetZipCodePanel" runat="Server" Visible="False">
          You can personalize this page by entering your Zip Code: 
          <asp:TextBox id="ZipCodeTextBox" Columns="5" MaxLength="5" runat="Server" />
          <asp:LinkButton id="EnterZipCodeButton" Runat="Server" Text="Go"
                          OnClick="EnterZipCode_OnClick" />
        </asp:Panel>
      </td>
    </tr>
  </table>
</form>

</body>
</html>

The following code example defines a class that inherits from the ProfileBase class to create a custom profile. The type of the custom profile is specified in the inherits attribute of the profile configuration element in the Web.config file for an application.

Security noteSecurity Note

This example contains a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

Imports System
Imports System.Web.Profile

Namespace Samples.AspNet.Profile

  Public Class EmployeeProfile
    Inherits ProfileBase

    <SettingsAllowAnonymous(False)> _
    <ProfileProvider("EmployeeInfoProvider")> _
    Public Property Department As String 
      Get 
        Return MyBase.Item("EmployeeDepartment").ToString()
      End Get 
      Set 
        MyBase.Item("EmployeeDepartment") = value
      End Set 
    End Property

    <SettingsAllowAnonymous(False)> _
    <ProfileProvider("EmployeeInfoProvider")> _
    Public Property Details As EmployeeInfo
      Get 
        Return CType(MyBase.Item("EmployeeInfo"), EmployeeInfo)
      End Get 
      Set 
        MyBase.Item("EmployeeInfo") = value
      End Set 
    End Property 
  End Class 

  Public Class EmployeeInfo
    Public Name As String 
    Public Address As String 
    Public Phone As String 
    Public EmergencyContactName As String 
    Public EmergencyContactAddress As String 
    Public EmergencyContactPhone As String 
  End Class 

End Namespace

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft