购物车(实现匿名用户向注册用户迁移)

    在电子商务网站,用户大多喜欢匿名浏览商品,部分用户可能会选择一些商品直到结帐时候才登陆以确认身份,也就是说应用程序需要实现匿名用户的数据迁移到经过身份验证的注册用户。
    通过ProfileModule.MigrateAnonymous 事件可以完成这个功能。 
    当匿名使用应用程序的用户登录时,可以使用 MigrateAnonymous 事件将配置文件属性值从匿名配置文件中复制到已验证身份的配置文件中。
在启动启用了用户配置文件的应用程序时,ASP.NET 会创建一个类型为 ProfileCommon 的新类,该类从 ProfileBase 类继承。强类型访问器被添加到 profile 配置节中为每个属性定义的 ProfileCommon 类中。GetProfile 方法使您能够根据用户名检索 ProfileCommon 对象。可以使用当前经过身份验证的配置文件的 GetProfile 方法来检索匿名配置文件的属性值。然后可以将匿名属性值复制到已验证身份的用户的当前配置文件中。
    ProfileModule.MigrateAnonymous 事件应用简单示例   

<configuration>
  
<system.web>
    
<authentication mode="Forms" >
      
<forms loginUrl="login.aspx" name=".ASPXFORMSAUTH" />
    
</authentication>

    
<anonymousIdentification enabled="true" />

    
<profile enabled="true" defaultProvider="AspNetSqlProvider">
      
<properties>
        
<add name="ZipCode" allowAnonymous="true" />
        
<add name="CityAndState" allowAnonymous="true" />
        
<add name="StockSymbols" type="System.Collections.ArrayList" allowAnonymous="true" />
      
</properties>
    
</profile>
  
</system.web>
</configuration>

    下面的代码示例演示 ASP.NET 应用程序的 Global.asax 文件中包含的 MigrateAnonymous 事件。MigrateAnonymous 事件将配置文件属性值从匿名配置文件复制到当前用户的配置文件。

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

    改进的购物车:
Web.config

Global.asax

代码下载
代码出自《asp.net2.0开发指南》。
posted on 2006-10-25 15:41  头发乱了  阅读(1364)  评论(1编辑  收藏  举报