ProfileBase クラス
アセンブリ: System.Web (system.web.dll 内)
構文
解説ASP.NET では、ProfileBase クラスを使用して、ユーザー プロファイルに対して使用されるクラスを作成します。ユーザー プロファイルが有効にされたアプリケーションを起動すると、ASP.NET によって、ProfileBase クラスを継承する ProfileCommon 型の新しいクラスが作成されます。profile 構成セクションに定義されているそれぞれのプロパティに対して、厳密に型指定されたアクセサが ProfileCommon クラスに追加されます。ProfileCommon クラスの厳密に型指定されたアクセサは、ProfileBase 基本クラスの GetPropertyValue メソッドおよび SetPropertyValue メソッドを呼び出して、プロファイルのプロパティ値の取得および設定をそれぞれ行います。ProfileCommon クラスのインスタンスは、ASP.NET アプリケーションの Profile プロパティ値として設定されます。
ASP.NET アプリケーションでユーザー プロファイルのインスタンスを作成するには、Create メソッドの使用をお勧めします。
継承時の注意 カスタムのプロファイル実装は、ProfileBase 抽象クラスを継承し、profile 構成要素で指定されないプロパティをユーザー プロファイルに定義することで作成できます。ユーザー プロファイルのカスタム型を Web.config ファイル内で指定するには、次の例に示すように、profile 構成要素の inherits 属性を使用します。EmployeeProfile クラスのコードは、このトピックの「使用例」にあります。<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>
使用例string 型の ZipCode プロパティおよび StringCollection 型の RecentSearchList プロパティを含んだユーザー プロファイルを指定する Web.config のコード例を次に示します。生成される現在の HttpContext の Profile プロパティは、指定されている各プロパティに対して、厳密に型指定されたアクセサを持ちます。
<configuration> <connectionStrings> <add name="SqlServices" connectionString= "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=aspnetdb;" /> </connectionStrings> <system.web> <anonymousIdentification enabled="true" /> <profile enabled="true" defaultProvider="SqlProvider" > <providers> <add name="SqlProvider" connectionStringName="SqlServices" applicationName="ProfileBaseApplication" type="System.Web.Profile.SqlProfileProvider" /> </providers> <properties> <add name="ZipCode" allowAnonymous="true" /> <add name="RecentSearchList" type="System.Collections.Specialized.StringCollection" serializeAs="Xml" allowAnonymous="true" /> </properties> </profile> </system.web> </configuration>
ユーザー プロファイルに対して、指定された ZipCode プロパティを読み込んだり、設定したりする ASP.NET ページのコード例を次に示します。
<%@ Page Language="VB" %> <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> <head> <title>Home Page</title> </head> <body> <form 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="C#" %> <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> <head> <title>Home Page</title> </head> <body> <form 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 クラスから継承するクラスを定義して、カスタム プロファイルを作成するコード例を次に示します。カスタム プロファイルの型は、アプリケーションの Web.config ファイルにある profile 構成要素の inherits 属性を使用して指定します。
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
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; } }
.NET Framework のセキュリティ
継承階層System.Configuration.SettingsBase
System.Web.Profile.ProfileBase
System.Web.Profile.DefaultProfile
スレッド セーフ
プラットフォームWindows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照- ProfileBase クラスのページへのリンク