The last time I worked with Profiles was back on the days we were creating web sites. Recently I had to add support for profiles in an old school asp.net web application. I tried to remember what I was doing back in the days, and I finally figured out that the code generation feature of Profile does not work in web application (only web sites). This means that you have to manually to the casting from
HttpContext.Current.Profile.GetPropertyValue and even worse you have to call a SetPropertyValue method to set the Profile property which is really ugly! The good news is that you can still use the inherits property of the Profile configuration and set your own class, instead of specifying the properties inside the web.config.
Here is a fast implementation I did to wrap up the whole functionality:
HttpContext.Current.Profile.GetPropertyValue and even worse you have to call a SetPropertyValue method to set the Profile property which is really ugly! The good news is that you can still use the inherits property of the Profile configuration and set your own class, instead of specifying the properties inside the web.config.
Here is a fast implementation I did to wrap up the whole functionality:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// To register, go to web.config and set | |
/// <profile enabled="true" inherits="Namespace.CustomProfile" > | |
/// </summary> | |
public class CustomProfile:ProfileBase | |
{ | |
/// <summary> | |
/// Needs <anonymousIdentification enabled="true"/> in system.web in order to track anonymous | |
/// </summary> | |
/// <returns></returns> | |
public static CustomProfile GetCurrent() | |
{ | |
if (HttpContext.Current.Profile.IsAnonymous) | |
return ProfileBase.Create(HttpContext.Current.Request.AnonymousID, false) as CustomProfile; | |
else | |
return ProfileBase.Create(HttpContext.Current.Profile.UserName) as CustomProfile; | |
} | |
/// <summary> | |
/// In the global asax add an event handler with the following signature | |
/// void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args) | |
/// and call this method in the body | |
/// </summary> | |
/// <param name="args"></param> | |
public static void MigrateAnonymous(ProfileMigrateEventArgs args) | |
{ | |
CustomProfile anonymousProfile = ProfileBase.Create(args.AnonymousID) as CustomProfile; | |
CustomProfile currentProfile = GetCurrent(); | |
currentProfile.DisplayName = anonymousProfile.DisplayName; | |
// 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); | |
} | |
[SettingsAllowAnonymous(true)] | |
public string DisplayName | |
{ | |
get | |
{ | |
return (string)base["DisplayName"]; | |
} | |
set | |
{ | |
base["DisplayName"] = value; | |
base.Save(); // Save changes | |
} | |
} | |
} |
No comments:
Post a Comment