Created
October 11, 2013 19:54
-
-
Save andreasbotsikas/6941008 to your computer and use it in GitHub Desktop.
Working with Asp.Net in web applications
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment