ProfileBase Classe
Definizione
Fornisce l'accesso non tipizzato alle informazioni e ai valori delle proprietà del profilo.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
- Ereditarietà
- Derivato
Esempio
Nell'esempio di codice seguente viene illustrato un file di Web.config che specifica un profilo utente che contiene una ZipCode
proprietà di tipo string
e una RecentSearchList
proprietà di tipo StringCollection .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. La Profile proprietà generata dell'oggetto corrente HttpContext avrà funzioni di accesso fortemente tipizzate per ogni proprietà specificata.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>
Nell'esempio seguente viene illustrata una pagina ASP.NET che legge e imposta la ZipCode
proprietà specificata per il profilo utente.The following example shows an ASP.NET page that reads and sets the ZipCode
property specified for the user profile. Prima di provare a eseguire questo codice, impostare il provider sul valore predefinito AspNetSqlProvider
nelle impostazioni di configurazione di ASP.NET per il sito Web.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>
Nell'esempio di codice seguente viene definita una classe che eredita dalla ProfileBase classe per creare un profilo personalizzato.The following code example defines a class that inherits from the ProfileBase class to create a custom profile. Il tipo del profilo personalizzato viene specificato nell' inherits
attributo dell'elemento di configurazione del profilo nel file Web.config per un'applicazione.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.
Importante
Questo esempio contiene una casella di testo che accetta l'input dell'utente, che rappresenta una potenziale minaccia per la sicurezza.This example contains a text box that accepts user input, which is a potential security threat. Per impostazione predefinita, le pagine Web ASP.NET verificano che l'input dell'utente non includa script o elementi HTML.By default, ASP.NET Web pages validate that user input does not include script or HTML elements. Per altre informazioni, vedere Cenni preliminari sugli attacchi tramite script.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
Commenti
ASP.NET usa la ProfileBase classe per creare la classe usata per il profilo utente.ASP.NET uses the ProfileBase class to create the class used for the user profile. Quando viene avviata un'applicazione con il profilo utente abilitato, ASP.NET crea una nuova classe di tipo ProfileCommon
, che eredita dalla ProfileBase classe.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. Le funzioni di accesso fortemente tipizzate vengono aggiunte alla ProfileCommon
classe per ogni proprietà definita nella sezione di configurazione del profilo .Strongly typed accessors are added to the ProfileCommon
class for each property defined in the profile configuration section. Le funzioni di accesso fortemente tipizzate della ProfileCommon
classe chiamano GetPropertyValue i SetPropertyValue metodi e della ProfileBase classe di base per recuperare e impostare rispettivamente i valori delle proprietà del profilo.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. Un'istanza della ProfileCommon
classe viene impostata come valore della Profile proprietà per l'applicazione ASP.NET.An instance of the ProfileCommon
class is set as the value of the Profile property for the ASP.NET application.
Per creare un'istanza di un profilo utente in un'applicazione ASP.NET, è consigliabile usare il Create metodo.To create an instance of a user profile in an ASP.NET application, it is recommended that you use the Create method.
Note per gli erediNotes to Inheritors
È possibile creare un'implementazione del profilo personalizzato che eredita dalla ProfileBase classe astratta e definisce le proprietà del profilo utente che non sono specificate nell'elemento di configurazione del profilo .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. È possibile specificare un tipo di profilo utente personalizzato nel file web.config con l' inherits
attributo dell'elemento di configurazione del profilo , come illustrato nell'esempio seguente.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. Il codice per la EmployeeProfile
classe è incluso nella sezione esempio di questo argomento.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>
Costruttori
ProfileBase() |
Crea un'istanza della classe ProfileBase.Creates an instance of the ProfileBase class. |
Proprietà
Context |
Ottiene il contesto delle impostazioni associato.Gets the associated settings context. (Ereditato da SettingsBase) |
IsAnonymous |
Ottiene un valore che indica se il profilo utente è relativo a un utente anonimo.Gets a value indicating whether the user profile is for an anonymous user. |
IsDirty |
Ottiene un valore che indica se alcune proprietà del profilo sono state modificate.Gets a value indicating whether any of the profile properties have been modified. |
IsSynchronized |
Ottiene un valore che indica se l'accesso all'oggetto è sincronizzato (thread-safe).Gets a value indicating whether access to the object is synchronized (thread safe). (Ereditato da SettingsBase) |
Item[String] |
Ottiene o imposta il valore di una proprietà del profilo indicizzato dal nome della proprietà.Gets or sets a profile property value indexed by the property name. |
LastActivityDate |
Ottiene la data e l'ora dell'ultima modifica o lettura del profilo.Gets the most recent date and time that the profile was read or modified. |
LastUpdatedDate |
Ottiene la data e l'ora dell'ultima modifica del profilo.Gets the most recent date and time that the profile was modified. |
Properties |
Ottiene un insieme di oggetti SettingsProperty per ciascuna proprietà del profilo.Gets a collection of SettingsProperty objects for each property in the profile. |
PropertyValues |
Ottiene un insieme di valori delle proprietà di impostazione.Gets a collection of settings property values. (Ereditato da SettingsBase) |
Providers |
Ottiene un insieme di provider di impostazioni.Gets a collection of settings providers. (Ereditato da SettingsBase) |
UserName |
Ottiene il nome utente relativo al profilo.Gets the user name for the profile. |
Metodi
Create(String) |
Viene utilizzato da ASP.NET per creare un'istanza di un profilo per il nome utente specificato.Used by ASP.NET to create an instance of a profile for the specified user name. |
Create(String, Boolean) |
Viene utilizzato da ASP.NET per creare un'istanza di un profilo per il nome utente specificato.Used by ASP.NET to create an instance of a profile for the specified user name. Questo metodo accetta un parametro che indica se l'utente è autenticato o anonimo.Takes a parameter indicating whether the user is authenticated or anonymous. |
Equals(Object) |
Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object. (Ereditato da Object) |
GetHashCode() |
Funge da funzione hash predefinita.Serves as the default hash function. (Ereditato da Object) |
GetProfileGroup(String) |
Ottiene un gruppo di proprietà identificate da un nome di gruppo.Gets a group of properties identified by a group name. |
GetPropertyValue(String) |
Ottiene il valore di una proprietà del profilo.Gets the value of a profile property. |
GetType() |
Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance. (Ereditato da Object) |
Initialize(SettingsContext, SettingsPropertyCollection, SettingsProviderCollection) |
Inizializza le proprietà interne utilizzate dall'oggetto SettingsBase.Initializes internal properties used by SettingsBase object. (Ereditato da SettingsBase) |
Initialize(String, Boolean) |
Inizializza le informazioni e i valori delle proprietà del profilo per l'utente corrente.Initializes the profile property values and information for the current user. |
MemberwiseClone() |
Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object. (Ereditato da Object) |
Save() |
Aggiorna l'origine dati del profilo in base ai valori delle proprietà del profilo modificati.Updates the profile data source with changed profile property values. |
SetPropertyValue(String, Object) |
Imposta il valore di una proprietà del profilo.Sets the value of a profile property. |
ToString() |
Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object. (Ereditato da Object) |