ASP.NET用户个性化设置Profile——匿名用户向注册用户迁移
时间:2010-10-18 来源:彬
在实现本例的过程中,关键是实现了ProfileModule类中的MigrateAnonymous事件。该事件在匿名用户登录时,且Profile中存在数据时触发。与其他事件不同的是,该事件处理程序必须在Global.asax文件中定义。
具体做法如下:
在项目中添加一个Global.asax文件,复制以下代码到文件当中
void Profile_MigrateAnonymous(object sender, ProfileMigrateEventArgs pe)
{
//取得该用户在匿名状态下的数据
ProfileCommon pc = Profile.GetProfile(pe.AnonymousID);
//判断购物车中是否添加了数据
if (pc.Cart.Count!=0)
{
//如果有数据,则将匿名状态下添加的商品添加到登陆后的购物车当中
//注意;Profile.Cart存放的是登陆后的数据
//pc.Cart是该用户在匿名状态下添加的数据
Profile.Cart = pc.Cart;
}
//删除匿名用户在aspnet_users表中的记录
Membership.DeleteUser(pe.AnonymousID);
//删除匿名用户Profile数据
ProfileManager.DeleteProfile(pe.AnonymousID);
//删除匿名用户标识
AnonymousIdentificationModule.ClearAnonymousIdentifier();
}
ok.就这么简单~~
相关阅读 更多 +










