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

ASP.NET Profile 屬性完整指南:原理、身份識別、提供者與企業級應用實踐

     編輯:關於ASP.NET

ASP.NET Profile 提供強型別、自動持久化的使用者設定檔儲存機制,是建立個人化 Web 應用的關鍵基礎設施。

Profile 運作原理

  • 執行階段根據 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>

安全與效能考量

  • Cookie 超時設大值可實現「永久」Profile,但需啟用 requireSSL="true" 並加密 Cookie(protection="All")。
  • 大型系統請執行 aspnet_regsql 建立專用資料庫,避免使用本地 MDF。
  • Profile 資料表結構支援跨應用程式共用,可作為 SSO(單一登入)的身份儲存基礎。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved