ICaramba

Miguel Castro's blog about .NET and its effect on National Security, the Eco-system, and his daughter's sleeping patterns.


News

My Stats

  • Posts - 122
  • Comments - 68
  • Trackbacks - 145

Twitter












Recent Comments


Recent Posts


Archives



Blogs I read


Links


Microsoft DCCs


 

The Profile system in ASP.NET 2.0 is one of the coolest and handiest additions that the boys in Redmond added to ASP.NET.  In fact I use it quite extensively.  A very nice feature is the ability to use Profiles when not authenticated.  This is called anonymous profiles.  I'm not going to show you how to set up profiles in this posting cause there's plenty of information out there about that.  What I wanted to talk about was the migration of anonymous profiles.  This is a feature that can be used for example, in a shopping cart application.  A user can add items to a shopping cart without yet being authenticated.  The important thing is to make sure that when the user authenticates to complete the checkout process, the shopping cart be moved over to the user's profile.  This is done in the Global.asax with code that looks like this:

void Profile_MigrateAnonymous(object sender, ProfileMigrateEventArgs e)
{
    ProfileCommon anonymousProfile = Profile.GetProfile(e.AnonymousID);
    if (anonymousProfile != null && anonymousProfile.Cart != null)
    {
        foreach (CorporateSite.Business.CartItem o_Item in anonymousProfile.Cart)
        {
               Profile.Cart.Add(o_Item);
        }
        Profile.Cart.Compress();
        Profile.Save();
        ProfileManager.DeleteProfile(anonymousProfile.UserName);
        AnonymousIdentificationModule.ClearAnonymousIdentifier();
    }
}

This event gets fired when a user authenticates.  As you can see, you can easily access the anonymous profile and move the items you want into the actual authenticated user's profile.  The detail I have found many explanations of this leave out is the last couple of lines.  They delete the anonymous profile from the database.  Leaving these lines out will cause the information to continue to persist in the anonymous profile and be again added to the user's profile everytime authentication takes place.

Incidentally, the Compress method in my code, compresses duplicate items in the shopping cart, accumulating their quantities.

Bye for now

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati