ASP.NET Profile 提供強型別、自動持久化的使用者設定檔儲存機制,是建立個人化 Web 應用的關鍵基礎設施。
web.config 中的屬性定義,動態產生繼承自 ProfileBase 的 ProfileCommon 類別。HttpContext.Profile 屬性自動指向該類別的實例,支援 Profile.PropertyName 強型別存取。ASPNETDB.MDF(SQL Express)。| 模式 | 識別方式 | 適用場景 |
|---|---|---|
| 已驗證使用者 | Forms/Windows/Passport 驗證後的 User.Identity.Name | 會員制網站、後台管理 |
| 匿名使用者 | GUID 字串,儲於 Cookie(.ASPXANONYMOUS) | 購物車、個人化首頁、試用功能 |
匿名使用者可透過 Profile.MigrateAnonymous 事件在登入時遷移資料至正式帳號。
<configuration>
<connectionStrings>
<add name="ProfileConn" connectionString="Server=...;Database=...;" />
</connectionStrings>
<system.web>
<anonymousIdentification enabled="true" cookieName=".ASPXANONYMOUS" cookieTimeout="30" />
<profile defaultProvider="SqlProfileProvider" inherits="MyApp.ProfileCommon">
<providers>
<add name="SqlProfileProvider"
type="System.Web.Profile.SqlProfileProvider"
connectionStringName="ProfileConn"
applicationName="/MyApp" />
</providers>
<properties>
<add name="Theme" type="string" defaultValue="Blue" />
<add name="PageSize" type="int" defaultValue="20" />
<add name="Preferences" type="MyApp.UserPreferences" serializeAs="Xml" />
</properties>
</profile>
</system.web>
</configuration>
requireSSL="true" 並加密 Cookie(protection="All")。aspnet_regsql 建立專用資料庫,避免使用本地 MDF。