ASP.Net Profiles, Anonymous Users and ProfileCommon

by john 11/25/2007 5:33:00 AM

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.

 

Currently rated 1.0 by 1 people

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Delphi | Providers | Blackfish | ASP.Net 2.0

Related posts

Comments

11/26/2007 3:52:35 PM

http://

Hi John
Thanks for your code.I have downloaded it & will try it out today
What do you eman by saying that Delphi does not support web sites & supports only web applications ?Can you plese clarify?
Does C# support web sites?

http://

11/27/2007 5:38:18 PM

http://

Visual Studio 2005 introduced the concept of "web site projects"

There is a comparison of the 2 here

http://msdn2.microsoft.com/en-us/library/Aa730880(VS.80).aspx

http://

12/1/2007 4:02:37 AM

http://

Hi!
Cool post, thanks a lot!

However I use c# and it not quite clear to me how you managed to substitute one class with another.
I mean ProfileCommon still will be generated in temp directory and it will lead to naming conflicts. I understand Delphi code, but unable compile it and see in details whats going on, because I do not have Delphi.

I still interested to reproduce it in C#. I did almost the same, but inherited my profile from custom class. I do not fill that I'm satisfied with this approach, because I cannot configure profile in config this way.

http://

12/7/2007 8:57:50 AM

http://

CNemo, I'm not substituting one class for another. I'm adding additional properties and methods to an existing class. C# 3.0 has something I believe that is similar called "extension methods".

http://

Comments are closed

Powered by BlogEngine.NET 1.3.0.0
Theme by Mads Kristensen

About the author

Name of author John Moshakis
I'm a software developer living in Toronto..

E-mail me Send mail

Calendar

<<  September 2020  >>
MoTuWeThFrSaSu
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

View posts in large calendar

Pages

    Recent posts

    Recent comments

    Authors

    Tags

    Don't show

      Disclaimer

      The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

      © Copyright 2020

      Sign in