ProfileBase ProfileBase ProfileBase ProfileBase Class

定義

提供設定檔屬性值和資訊的不具型別存取。Provides untyped access to profile property values and information.

public ref class ProfileBase : System::Configuration::SettingsBase
public class ProfileBase : System.Configuration.SettingsBase
type ProfileBase = class
    inherit SettingsBase
Public Class ProfileBase
Inherits SettingsBase
繼承
衍生

範例

下列程式碼範例所示範的 web.config 檔案, 會指定ZipCode包含型string別屬性和RecentSearchListStringCollection別之屬性的使用者設定檔。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. Profile 目前HttpContext產生的屬性會有每個指定屬性的強型別存取子。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>  

下列範例顯示的 ASP.NET 網頁會讀取和設定ZipCode為使用者設定檔指定的屬性。The following example shows an ASP.NET page that reads and sets the ZipCode property specified for the user profile. 嘗試執行此程式碼之前, 請先將提供者設定AspNetSqlProvider為網站 [ASP.NET 設定] 中的預設值。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="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

public void Page_PreRender()
{
  if (Profile.ZipCode == null)
  {
    PersonalizePanel.Visible = false;
    GetZipCodePanel.Visible = true;
  }
  else
  {
    ZipCodeLabel.Text = Profile.ZipCode;

    // Get personalized information for zip code here.

    PersonalizePanel.Visible = true;
    GetZipCodePanel.Visible = false;
  }
}

public void ChangeZipCode_OnClick(object sender, EventArgs args)
{
  ZipCodeTextBox.Text = Profile.ZipCode;
  Profile.ZipCode = null;

  PersonalizePanel.Visible = false;
  GetZipCodePanel.Visible = true;
}

public void EnterZipCode_OnClick(object sender, EventArgs args)
{
  Profile.ZipCode = ZipCodeTextBox.Text;
}

</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>
<%@ 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>

下列程式碼範例會定義繼承ProfileBase自類別的類別, 以建立自訂設定檔。The following code example defines a class that inherits from the ProfileBase class to create a custom profile. 自訂設定檔的類型是在應用程式inherits的 web.config 檔案中,配置檔設定元素的屬性中指定。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.

重要

此範例包含可接受使用者輸入的文字方塊, 這是潛在的安全性威脅。This example contains a text box that accepts user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

using System;
using System.Web.Profile;

namespace Samples.AspNet.Profile
{
  public class EmployeeProfile : ProfileBase
  {
    [SettingsAllowAnonymous(false)]
    [ProfileProvider("EmployeeInfoProvider")]
    public string Department
    {
      get { return base["EmployeeDepartment"].ToString(); }
      set { base["EmployeeDepartment"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    [ProfileProvider("EmployeeInfoProvider")]
    public EmployeeInfo Details
    {
      get { return (EmployeeInfo)base["EmployeeInfo"]; }
      set { base["EmployeeInfo"] = value; }
    }

  }

  public class EmployeeInfo
  {
    public string Name;
    public string Address;
    public string Phone;
    public string EmergencyContactName;
    public string EmergencyContactAddress;
    public string EmergencyContactPhone;
  }
}
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

備註

ASP.NET 會使用ProfileBase類別來建立使用者設定檔所使用的類別。ASP.NET uses the ProfileBase class to create the class used for the user profile. 當已啟用使用者設定檔的應用程式啟動時, ASP.NET 會建立類型ProfileCommon的新類別, 它會繼承ProfileBase自類別。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. 針對 [設定檔設定] 區段ProfileCommon中所定義的每個屬性, 將強型別存取子新增至類別。Strongly typed accessors are added to the ProfileCommon class for each property defined in the profile configuration section. ProfileCommon類別的強型別存取子會呼叫GetPropertyValue ProfileBase基類SetPropertyValue的和方法, 分別取得和設定配置檔案屬性值。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. ProfileCommon類別的實例會設定為 ASP.NET 應用程式的Profile屬性值。An instance of the ProfileCommon class is set as the value of the Profile property for the ASP.NET application.

若要在 ASP.NET 應用程式中建立使用者設定檔的實例, 建議您使用Create方法。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

您可以建立自訂設定檔的執行, 此ProfileBase衍生繼承自抽象類別別, 並定義配置檔設定元素中未指定之使用者設定檔的屬性。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. 您可以使用inherits 配置檔設定元素的屬性, 在 web.config 檔案中指定自訂的使用者設定檔類型, 如下列範例所示。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. EmployeeProfile類別的程式碼包含在本主題的範例一節中。The code for the EmployeeProfile class is included in the Example section of this topic.

<configuration>
   <system.web>
      <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>  
   </system.web>
</configuration>

建構函式

ProfileBase() ProfileBase() ProfileBase() ProfileBase()

建立 ProfileBase 類別的執行個體。Creates an instance of the ProfileBase class.

屬性

Context Context Context Context

取得關聯的設定內容。Gets the associated settings context.

(Inherited from SettingsBase)
IsAnonymous IsAnonymous IsAnonymous IsAnonymous

取得布林值,指出使用者設定檔是否供匿名使用者使用。Gets a value indicating whether the user profile is for an anonymous user.

IsDirty IsDirty IsDirty IsDirty

取得布林值,指出設定檔屬性是否已修改。Gets a value indicating whether any of the profile properties have been modified.

IsSynchronized IsSynchronized IsSynchronized IsSynchronized

取得值,指出對物件的存取是否為同步的 (安全執行緒)。Gets a value indicating whether access to the object is synchronized (thread safe).

(Inherited from SettingsBase)
Item[String] Item[String] Item[String] Item[String]

取得或設定由屬性名稱索引的設定檔屬性值。Gets or sets a profile property value indexed by the property name.

LastActivityDate LastActivityDate LastActivityDate LastActivityDate

取得設定檔最近讀取或修改的日期和時間。Gets the most recent date and time that the profile was read or modified.

LastUpdatedDate LastUpdatedDate LastUpdatedDate LastUpdatedDate

取得設定檔最近修改的日期和時間。Gets the most recent date and time that the profile was modified.

Properties Properties Properties Properties

取得設定檔中每一個屬性的 SettingsProperty 物件集合。Gets a collection of SettingsProperty objects for each property in the profile.

PropertyValues PropertyValues PropertyValues PropertyValues

取得設定屬性值的集合。Gets a collection of settings property values.

(Inherited from SettingsBase)
Providers Providers Providers Providers

取得設定提供者的集合。Gets a collection of settings providers.

(Inherited from SettingsBase)
UserName UserName UserName UserName

取得設定檔的使用者名稱。Gets the user name for the profile.

方法

Create(String) Create(String) Create(String) Create(String)

ASP.NET 用來建立指定使用者名稱的設定檔執行個體。Used by ASP.NET to create an instance of a profile for the specified user name.

Create(String, Boolean) Create(String, Boolean) Create(String, Boolean) Create(String, Boolean)

ASP.NET 用來建立指定使用者名稱的設定檔執行個體。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.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetProfileGroup(String) GetProfileGroup(String) GetProfileGroup(String) GetProfileGroup(String)

取得由群組名稱識別的屬性群組。Gets a group of properties identified by a group name.

GetPropertyValue(String) GetPropertyValue(String) GetPropertyValue(String) GetPropertyValue(String)

取得設定檔屬性的值。Gets the value of a profile property.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
Initialize(SettingsContext, SettingsPropertyCollection, SettingsProviderCollection) Initialize(SettingsContext, SettingsPropertyCollection, SettingsProviderCollection) Initialize(SettingsContext, SettingsPropertyCollection, SettingsProviderCollection) Initialize(SettingsContext, SettingsPropertyCollection, SettingsProviderCollection)

初始化 SettingsBase 物件所用的內部屬性。Initializes internal properties used by SettingsBase object.

(Inherited from SettingsBase)
Initialize(String, Boolean) Initialize(String, Boolean) Initialize(String, Boolean) Initialize(String, Boolean)

初始化目前使用者的設定檔屬性值和資訊。Initializes the profile property values and information for the current user.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Save() Save() Save() Save()

使用已變更的設定檔屬性值,更新設定檔資料來源。Updates the profile data source with changed profile property values.

SetPropertyValue(String, Object) SetPropertyValue(String, Object) SetPropertyValue(String, Object) SetPropertyValue(String, Object)

設定設定檔屬性的值。Sets the value of a profile property.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於

另請參閱