Asp.net

遷移匿名配置文件的最佳方式

  • January 25, 2010

是否有其他方法可以隱式遷移所有參數?或任何其他優點。

來自MSDN

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);

}

或者這是最好/唯一的方法?

這是要走的路。但我會建議一個概括。您可以遍歷ProfileBase.Properties集合,而不是對每個屬性進行硬編碼。這些方面的東西:

var anonymousProfile = Profile.GetProfile(args.AnonymousID);
foreach(var property in anonymousProfile.PropertyValues)
{
   Profile.SetPropertyValue(property.Name, property.PropertyValue);
}

由於屬性組被表示為屬性名稱的一部分(例如,“Settings.Theme”表示設置組中的主題屬性),上述程式碼也應該與屬性組一起使用。

引用自:https://stackoverflow.com/questions/1660999