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 2.3 by 3 people

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

Tags:

Blackfish | ASP.Net 2.0 | Delphi | Providers

Starting WCF Programming in Delphi 2007

by john 11/7/2007 8:42:00 AM

I have just started looking into Windows Communication Foundation.

Pawel Glowacki created a really nice article on the CodeGear Developer Network titled Introduction to WCF Programming in Delphi.

I'm particularly interested in web development and I noticed that there is a WCF Service Template in Visual Studio 2005, so I have converted it to Delphi.

I have uploaded the template to CodeCentral.

If you are not sure how to use the template, I have a blog post about templates and how you install them,

Once installed you can now create a new WCF Service application

 

You should end up with a new ASP.Net  Web Application with a single WCF Service.

 

Assuming that you have already installed Microsoft dotnet v3.0, you can now build and run the application.

The service description should now appear in a browser.

 

To exercise the web application you need to do the following:-

1) Create a new VCL.Net application

2) Add a reference to the System.ServiceModel assembly

3) Add

C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0

to the compiler search path

4) Your code might look something like

uses
  System.ServiceModel, Service, System.ServiceModel.Channels;

{$R *.nfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  aEndpointAddress: EndpointAddress;
  aBinding: WSHttpBinding;
  aFactory: ChannelFactory<IMyService>;
  aChannel: IMyService;
  someValue: string;
  value: DataContract1;
begin

  try
    aEndpointAddress := EndpointAddress.Create('http://localhost:8080/WCFServiceWebApplication1/Service.svc');
    aBinding := WSHttpBinding.Create;
    aFactory := ChannelFactory<IMyService>.Create(aBinding, aEndpointAddress);
    aChannel := aFactory.CreateChannel;
    try
      someValue:=aChannel.MyOperation1('John');
      ShowMessage(someValue);

      value:= DataContract1.Create;

      value.FirstName:='John';
      value.LastName:='Moshakis';

      aChannel.MyOperation2(value);
      ShowMessage(someValue);

    finally
      IChannel(aChannel).Close;
      IDisposable(aChannel).Dispose;
    end;
  except
    on E:Exception do
    begin
      raise;
    end;

  end;

end;
 

 A word of warning, if like me you use Cassini as your web server, I think you need to upgrade to the latest version. Below is a screenshot of the application running with the version of Cassini supplied with Delphi 2007.

 

 

Please read my blog post about upgrading Cassini.

Comments welcome 

 I also see that Pawel is doing a presentation at CodeRage II titled "Delphi for .NET WCF Programming: Deeper Dive", I'm looking forward to listening to that,

Currently rated 3.0 by 2 people

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

Tags:

ASP.Net 2.0 | WCF | Delphi | VCL.Net

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 2017  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

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 2017

      Sign in