Client Application Services and WPF

Although my sample for Client Application Services (CAS) uses Windows Forms, you can adapt it to Windows Presentation Foundation (WPF) with little effort. The configuration steps are the same, and the client source code is the same, although some of the code would go in WPF equivalents of Windows Forms event handlers (for example, using an App.Startup or Window.Loaded event handler rather than a Form.Load event handler).

However, there is a peculiarity in WPF that prevents things from just working without a small adjustment. After authentication using CAS, the static Thread.CurrentPrincipal property gets set to an instance of ClientRolePrincipal. Internally, CAS checks for this value to determine whether a user is still logged in. Unfortunately, in WPF, Thread.CurrentPrincipal reverts to the default value (a GenericPrincipal instance) some time after the Window.Loaded event.

For details, see this forum post. The fix for CAS is simple, however: just add the following line of code after your Membership.ValidateUser call in your App.Startup or Window.Loaded event handler:

AppDomain

.CurrentDomain.SetThreadPrincipal(Thread.CurrentPrincipal);

At this point, Thread.CurrentPrincipal is still set to the ClientRolePrincipal object. The SetThreadPrincipal method call is what makes it stay there.

Comments

  • Anonymous
    December 13, 2007
    Although my sample for Client Application Services (CAS) uses Windows Forms, you can adapt it to Windows