Quantcast
Channel: CSLA .NET
Viewing all articles
Browse latest Browse all 764

WinForms - ApplicationContext.User principal being lost

$
0
0

I've recently taken on a legacy project which involves using CSLA with WinForms as a thick client (i.e. CSLA Client and Server within same application).

It's currently running CSLA 4.0.1 - I have tried upgrading to 4.3.14, but my issue is still not resolved (I can't upgrade to 4.5.x yet as I will need to convert about 500 overrides of Save to SaveAsync!)

The issue is regarding the custom Principal which is being lost.  This is using custom authentication.

During login, the custom principal is set using ApplicationContext.User.  The custom principal derives from Csla.Security.CslaPrincipal.  The identity on the custom principal derives from ReadOnlyBase<T>.

Whilst everything "works", I have noticed that at times that the ApplicationContext.User has reverted to a GenericPrincipal object.  This is on the same thread that the login took place (in this case the UI thread).  I cannot find any places where we are setting the Thread.CurrentPrincipal or ApplicationContext.User directly (except post-login).

The previous developers seem to have been aware of this because they have "solved" the issue with this piece of code.  But of course if you want to get the Principal without running a data operation you cannot trust it to be set.

Csla.DataPortal.DataPortalInitInvoke += DataPortal_DataPortalInitInvoke;

void DataPortal_DataPortalInitInvoke(object obj)

{

   if (!ReferenceEquals(Csla.ApplicationContext.User, s_CurrentPrincipal))

      Csla.ApplicationContext.User = s_CurrentPrincipal;

}

Where s_CurrentPrincipal is a static field which I can confirm DOES keep the current principal.  I have put a trace on this and found that this handler does not set the principal to the GenericPrincipal so I don't believe this is the problem (this is the "solution"!)

The app.config I have ensure has the following:

   <add key="CslaAuthentication" value="Custom" />

 

But the Principal is still being lost.  

Is there anything I am missing?  Would there be any reason that the Thread.Principal is being lost after it has been set?  This is pure WinForms, so HttpContext should not be involved.


Viewing all articles
Browse latest Browse all 764

Trending Articles