Its easy storing user specific information using the profile feature. When writing web sites as opposed to web applications a strongly typed object called profile is added to the webpage. This object defines a mapping to the profile section in web.config.
For example
<profile defaultProvider="AspNetAdoProfileProvider">
<properties>
<group name="UserDetails" >
<add name="State" type="System.String" allowAnonymous="true" />
</group>
</properties>
</profile>
Would be
self Profile.UserDetails.State in your Delphi codebehind, unfortunately Delphi only supports web applications.
so your left with a more verbose version which would be
self.Context.Profile.GetProfileGroup('UserDetails').GetPropertyValue('State').ToString;
This is not something I'm going to easily remember, so its Class Helpers to the rescue again.
I started off by adding a Profile property to my webpage base class.
ProfileWebApplicationPage=class(Page)
public
function get_Profile: ProfileBase;
published
published
property Profile:ProfileBAse read get_Profile;
end;
function ProfileWebApplicationPage.get_Profile: ProfileBase;
begin
result:=self.Context.Profile;
end;
And I then introduced the first class helper for Profilebase
ProfileCommon=class helper for ProfileBase
public
function GetProfile(username:string):ProfileBase;virtual;
function get_ProfileGroupUserDetails: ProfileGroupBase;
published
property UserDetails:ProfileGroupBase read get_ProfileGroupUserDetails;
end;
and then a second for ProfileGroupBase
ProfileGroupUserDetails=class helper for ProfileGroupBase
published
public property Zip: string read get_Zip write set_Zip;
public property Address: string read get_Address write set_Address;
public property City: string read get_City write set_City;
public property Email: string read get_Email write set_Email;
public property GetNewsletter: boolean read get_GetNewsletter write set_GetNewsletter;
public property Phone: string read get_Phone write set_Phone;
public property State: string read get_State write set_State;
end;
And now the the following will appear in Code Completion and Compile
anonymousProfile := Profile.GetProfile(args.AnonymousID);
if (Profile.LastActivityDate = DateTime.MinValue)then
begin
Profile.UserDetails.Address := anonymousProfile.UserDetails.Address;
Profile.Save();
end;
The full source for the example can be found in codecentral. This example also demontrates how anonymous profiles can be migrated to a new website user using the Profile_OnMigrateAnonymous event in Global.asax
If you run the example default.aspx should be displayed in your browser
You have entered the site as an anonymous user, you can now click on the profile link and head over to the profile page. In this page you enter your profile and hit the save button.
You are now ready to use the create user menu option. If you enter your new user details and hit the create user button you end up here.
When the user hits the continue button the Profile_OnMigrateAnonymous event is fired and the code inside the event handler in Global.asax is executed.
anonymousProfile := Profile.GetProfile(args.AnonymousID);
if (Profile.LastActivityDate = DateTime.MinValue)then
begin
Profile.UserDetails.Address := anonymousProfile.UserDetails.Address;
Profile.UserDetails.City := anonymousProfile.UserDetails.City;
Profile.UserDetails.State := anonymousProfile.UserDetails.State;
Profile.UserDetails.Zip := anonymousProfile.UserDetails.Zip;
Profile.UserDetails.Email := anonymousProfile.UserDetails.Email;
Profile.UserDetails.Phone := anonymousProfile.UserDetails.Phone;
Profile.UserDetails.GetNewsletter := anonymousProfile.UserDetails.GetNewsletter;
Profile.Save();
ProfileManager.DeleteProfile(anonymousProfile.UserName);
AnonymousIdentificationModule.ClearAnonymousIdentifier();
This code migrates the profile for the anonymous user over to the newly created user.