Robust Custom Profiles with ASP.NET 2.0
September 25th, 2006While you can configure your websites to use a set of custom Profile properties you may not want to have ASP.NET do all of that serialization to the database for you. I personally feel that too much serialization of complex data will eventually bite you when you choose to upgrade your system or adjust your properties. I prefer to handle the data myself, so I bind the data to the Profile using the UserID I extract from the Membership provider.
The SQL Server implementation of the Membership provider uses a Guid to identify each user when authenticated. But the value for the Guid comes from a different source if the user is anonymous. I use the following code to extract the UserID reliably:
To ensure this works without popping an exception, be sure to have anonymous identification enabled with the anonymousIdentification element. Still, you may find that it fails so you may want to wrap that in a try...catch block just to be sure.
Finally, you need to handle the transition from anonymous to authenticated users. You can do so with the ProfileModule.MigrateAnonymous Event. At this point you can safely base your custom profile properties on that Guid value and share that data across many websites.
