共用方式為


ASP.NET 設定檔屬性的使用者識別

更新:2007 年 11 月

ASP.NET 使用者設定檔功能是用來提供目前使用者的唯一資訊。設定檔可以搭配驗證使用者或匿名 (非驗證) 使用者運作。

驗證使用者

根據預設,使用者設定檔與目前 HTTP 內容之 User 屬性中儲存的使用者識別相關,並且可經由 HttpContext.Current 屬性存取目前 HTTP 的內容。使用者識別是經由下列方式判定:

  • ASP.NET Forms 驗證系統,在成功驗證後會設定使用者識別。

  • Windows 或 Passport 驗證,在成功驗證後會設定使用者識別。

  • 自訂驗證,取得使用者認證然後手動設定使用者識別。

ASP.NET Forms 驗證涉及建立登入表單然後提示使用者輸入認證。您可以使用 ASP.NET 登入控制項建立登入表單,然後在不需撰寫任何程式碼的情況下執行表單驗證。如需使用 ASP.NET 功能驗證使用者的資訊,請參閱 ASP.NET 登入控制項概觀使用成員資格管理使用者。如需表單驗證的詳細資訊,請參閱 HOW TO:實作簡單表格驗證

匿名使用者

設定檔可以搭配匿名使用者運作。預設不會啟用匿名設定檔支援,所以您必須明確啟用它。此外,當您在 Web.config 檔中定義設定檔屬性時,您必須明確地讓匿名使用者能夠個別使用這些屬性。因為設定檔可能會用來搭配驗證使用者運作,所以設定檔屬性預設並不支援匿名存取,並且許多屬性很可能與匿名使用者無法使用的個人資訊有關。

如果啟用匿名識別,ASP.NET 會為第一次造訪網站的使用者建立唯一識別。唯一使用者識別會儲存在使用者電腦的 Cookie 中,就可以在提出每個網頁要求時識別使用者。Cookie 的預設到期會設定為大約 70 天,並且在使用者造訪網站時定期更新。如果使用者的電腦不接受 Cookie,雖然在使用者關閉瀏覽器時會遺失識別,但是使用者的識別仍然可以維護為網頁要求 URL 的一部分。

如需啟用匿名識別的資訊,請參閱 anonymousIdentification 項目 (ASP.NET 設定結構描述)

移轉匿名設定檔資訊

在一些情況中,您的應用程式可能一開始會維護匿名使用者的個人化資訊,但最後使用者會登入應用程式。在這種情況下,使用者的識別會從指派的匿名使用者識別,變更為驗證處理序提供的識別。

當使用者登入時 (換句話說,當他們不再是匿名使用者),就會引發 MigrateAnonymous 事件。您可以處理這個事件以便將資訊從使用者匿名識別,移轉為新的驗證識別 (如果需要的話)。下列程式碼範例,示範了當使用者通過驗證時如何移轉資訊。

Public Sub Profile_OnMigrateAnonymous(sender As Object, args As ProfileMigrateEventArgs)
  Dim anonymousProfile As ProfileCommon = Profile.GetProfile(args.AnonymousID)

  Profile.ZipCode = anonymousProfile.ZipCode
  Profile.CityAndState = anonymousProfile.CityAndState
  Profile.StockSymbols = anonymousProfile.StockSymbols

  ''''''''
  ' Delete the anonymous profile. If the anonymous ID is not 
  ' needed in the rest of the site, remove the anonymous cookie.

  ProfileManager.DeleteProfile(args.AnonymousID)
  AnonymousIdentificationModule.ClearAnonymousIdentifier()

  ' Delete the user row that was created for the anonymous user.
  Membership.DeleteUser(args.AnonymousID, True)
End Sub
public void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args)
{
  ProfileCommon anonymousProfile = Profile.GetProfile(args.AnonymousID);

  Profile.ZipCode = anonymousProfile.ZipCode;
  Profile.CityAndState = anonymousProfile.CityAndState;
  Profile.StockSymbols = anonymousProfile.StockSymbols;

  ////////
  // Delete the anonymous profile. If the anonymous ID is not 
  // needed in the rest of the site, remove the anonymous cookie.

  ProfileManager.DeleteProfile(args.AnonymousID);
  AnonymousIdentificationModule.ClearAnonymousIdentifier(); 

  // Delete the user row that was created for the anonymous user.
  Membership.DeleteUser(args.AnonymousID, true);

}

請參閱

概念

ASP.NET 設定檔屬性概觀

ASP.NET 設定檔屬性概觀

定義 ASP.NET 設定檔屬性

ASP.NET 設定檔提供者