首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET 2.0 Profile 個人化系統完整實作:從 SQL 設定到程式碼應用

     編輯:關於ASP.NET

ASP.NET 2.0 內建 `Profile` 系統,大幅簡化使用者個人化資料的儲存與存取。本文完整記錄從 SQL Server 環境建置、提供者配置、web.config 定義屬性、匿名/已驗證用戶支援、到 C# 程式碼實際讀寫的端到端實作流程。

架構總覽

Profile 系統
  ├─ ProfileBase (抽象基底)
  ├─ ProfileProvider (提供者模型)
  │    └─ SqlProfileProvider (預設,SQL Server 儲存)
  ├─ 屬性定義 (web.config )
  │    ├─ 型別、序列化方式、允許匿名
  │    └─ 動態編譯強型別 Profile 類別
  └─ 執行階段
       ├─ HttpContext.Profile 存取
       ├─ 匿名識別 (anonymousIdentification)
       └─ 自動儲存/載入 (模組攔截)

環境建置步驟

1. SQL Server 準備 (支援 7.0/2000/2005/2008/2012/2016/2019)

# 使用 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

2. IIS / ASP.NET 設定

  • IIS 管理員 → 網站屬性 → ASP.NET 標籤 → 編輯配置
  • 驗證標籤 → 角色管理 → 啟用
  • 一般標籤 → `LocalSqlServer` 連線字串,例如:
data source=MySqlServer;Integrated Security=SSPI;
Initial Catalog=aspnetdb;User Instance=false

web.config 定義 Profile 屬性

<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複雜物件、集合、DataSetXML 序列化
Binary標記 `[Serializable]` 的類別二進位序列化

C# 程式碼存取 Profile

ASPX 頁面 (含前端控制項)

<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" />

CodeBehind

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 群組` ` → `Profile.Address.City`
效能優化大型物件改用 `serializeAs="Binary"`,減少資料庫欄位大小;關閉不需要的自動儲存
ASP.NET Core 替代方案無內建 Profile,改用 `IDistributedCache` + 自訂服務、或 `Microsoft.AspNetCore.Identity` 擴充使用者實體

常見問題排查

  1. **Profile 屬性為 null**:確認 `web.config` 定義名稱大小寫、型別正確、Provider 連線字串可連線
  2. **匿名用戶無法儲存**:檢查 `` 且屬性 `allowAnonymous="true"`
  3. **資料庫未建立表格**:執行 `aspnet_regsql -A p` 完整安裝 Profile 架構
  4. **序列化失敗**:複雜型別必須標記 `[Serializable]` 或實作 `IXmlSerializable`

ASP.NET 2.0 Profile 系統雖屬 WebForms 時代產物,但其**提供者模型、強型別動態編譯、匿名/驗證無縫切換**的設計精髓,在現代 ASP.NET Core 自訂使用者資料儲存架構中依然閃耀著參考價值。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved