ASP.NET 2.0 內建 `Profile` 系統,大幅簡化使用者個人化資料的儲存與存取。本文完整記錄從 SQL Server 環境建置、提供者配置、web.config 定義屬性、匿名/已驗證用戶支援、到 C# 程式碼實際讀寫的端到端實作流程。
Profile 系統 ├─ ProfileBase (抽象基底) ├─ ProfileProvider (提供者模型) │ └─ SqlProfileProvider (預設,SQL Server 儲存) ├─ 屬性定義 (web.config) │ ├─ 型別、序列化方式、允許匿名 │ └─ 動態編譯強型別 Profile 類別 └─ 執行階段 ├─ HttpContext.Profile 存取 ├─ 匿名識別 (anonymousIdentification) └─ 自動儲存/載入 (模組攔截)
# 使用 aspnet_regsql.exe 建立應用程式服務資料庫
# 路徑:C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe
# 命令列模式 (Windows 驗證,建立預設 aspnetdb)
aspnet_regsql.exe -A p -E -S
# 參數說明:
# -A p : 新增 Profile 功能
# -E : 使用目前 Windows 認證
# -S : 指定 SQL Server 執行個體
# -d : (選用) 指定資料庫名稱,預設 aspnetdb
data source=MySqlServer;Integrated Security=SSPI;
Initial Catalog=aspnetdb;User Instance=false
<configuration>
<system.web>
<!-- 啟用匿名識別 -->
<anonymousIdentification enabled="true" />
<profile enabled="true" defaultProvider="SqlProfileProvider">
<providers>
<clear/>
<add name="SqlProfileProvider"
type="System.Web.Profile.SqlProfileProvider"
connectionStringName="LocalSqlServer"
applicationName="/" />
</providers>
<properties>
<!-- 字串型別,允許匿名 -->
<add name="FirstName" type="String" allowAnonymous="true" />
<add name="LastName" type="String" allowAnonymous="true" />
<!-- 複雜型別,需序列化 -->
<add name="Preferences" type="MyApp.UserPreferences"
serializeAs="Xml" allowAnonymous="false" />
<!-- 集合型別 -->
<add name="ShoppingCart" type="System.Collections.Generic.List<CartItem>"
serializeAs="Binary" />
</properties>
</profile>
</system.web>
<connectionStrings>
<add name="LocalSqlServer"
connectionString="Data Source=.;Integrated Security=SSPI;Initial Catalog=aspnetdb"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
| 模式 | 適用型別 | 儲存格式 |
|---|---|---|
| ProviderSpecific (預設) | 基本型別、DateTime、字串 | 屬性名:值:型別碼 管線分隔 |
| String | 實作 TypeConverter 的型別 | 字串表示 |
| Xml | 複雜物件、集合、DataSet | XML 序列化 |
| Binary | 標記 `[Serializable]` 的類別 | 二進位序列化 |
<asp:TextBox ID="txtFirstName" runat="server" />
<asp:TextBox ID="txtLastName" runat="server" />
<asp:Button ID="btnSave" Text="儲存" OnClick="btnSave_Click" runat="server" />
<asp:Button ID="btnLoad" Text="載入" OnClick="btnLoad_Click" runat="server" />
public partial class ProfileDemo : System.Web.UI.Page
{
protected void btnSave_Click(object sender, EventArgs e)
{
Profile.FirstName = txtFirstName.Text;
Profile.LastName = txtLastName.Text;
// Profile.Save(); // 通常自動儲存,除非設定 automaticSaveEnabled="false"
}
protected void btnLoad_Click(object sender, EventArgs e)
{
txtFirstName.Text = Profile.FirstName ?? "";
txtLastName.Text = Profile.LastName ?? "";
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 匿名用戶自動獲得匿名 ID,可直接存取
if (Profile.FirstName != null)
{
txtFirstName.Text = Profile.FirstName;
txtLastName.Text = Profile.LastName;
}
}
}
}
| 主題 | 關鍵點 |
|---|---|
| 匿名遷移至已驗證 | `Profile.MigrateAnonymous("UserName")` 在登入後呼叫,合併匿名資料 |
| 自訂 Profile 提供者 | 繼承 `ProfileProvider`,覆寫 `GetPropertyValues`/`SetPropertyValues`,支援 Oracle、MongoDB、Redis |
| Profile 群組 | ` |
| 效能優化 | 大型物件改用 `serializeAs="Binary"`,減少資料庫欄位大小;關閉不需要的自動儲存 |
| ASP.NET Core 替代方案 | 無內建 Profile,改用 `IDistributedCache` + 自訂服務、或 `Microsoft.AspNetCore.Identity` 擴充使用者實體 |
ASP.NET 2.0 Profile 系統雖屬 WebForms 時代產物,但其**提供者模型、強型別動態編譯、匿名/驗證無縫切換**的設計精髓,在現代 ASP.NET Core 自訂使用者資料儲存架構中依然閃耀著參考價值。