Help with accessing identity information in dataportal_fetch

Help with accessing identity information in dataportal_fetch

Old forum URL: forums.lhotka.net/forums/t/1978.aspx


MikeHamilton posted on Thursday, December 14, 2006

I am having some weird issues with accessing the UserID for the user logged in.

During testing I am getting an error using the following line in a DataPortal_Fetch method.

.Parameters.AddWithValue("@UserID", CType(Csla.User.Identity, Security.SecurityIdentity).ID)

The error is ...
Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'BlakeIS.FastTrack.BusinessLibrary.Security.SecurityIdentity'

...Security.SecurityIdentity inherits from ReadOnlyBase and implements iIdentity

The weird part is this line works fine in another BusinessObject. Both of them inherit from BusinessBase. the one difference is the failure happens in the DataPortal_Fetch but in the class where it works is the DataPortal_Insert. Is this the issue? (it is not valid in fetch methods)

Anyone got some ideas? or what code you need to see to help me out.

Thanks,

Mike

MikeHamilton replied on Thursday, December 14, 2006

Just wanted to add some more information... I was looking through my PDF of the book and can't find any use of Csla.User so I tried the following line...

.Parameters.AddWithValue("@UserID", CType(Csla.ApplicationContext.User.Identity, Security.SecurityIdentity).ID)

and that didn't work either. The same error gets thrown. And I am logged in (oops looks like maybe I am not... will do more testing on this... seems Csla.User.Identity.IsAuthenticated is False right before the line it fails on)

Mike

MikeHamilton replied on Thursday, December 14, 2006

OK so I am fairly sure I have figured out what the probelm is. It is the fact that this call is in an event that gets raised by a control that does it processing asynchronsly (sp?) so the event is being raised by different threads. Can someone verify this is the issue?

So what is the best way to handle this? Should I store a reference to the user on the main form and use that reference (if I can.. kinda of new on delaing with multiple threads)? Or is there some other way to do it?

Thanks,

Mike

RockfordLhotka replied on Thursday, December 14, 2006

CSLA .NET includes Csla.ApplicationContext.User - that's the only User property in the framework, and is the one I would think you'd be using. If you do have a Csla.User property, it is a customization you or someone on your team must have made.

If you are using custom authentication, which it sounds like you are, you must make sure that the principal object (Csla.ApplicationContext.User) is restored on each page request. Typically this is done in global.asax as shown in Chapter 10 in the book. After that point, Csla.ApplicationContext.User should provide access to the pricipal object in all your code.

MikeHamilton replied on Thursday, December 14, 2006

Thanks for the reply.

We (me as I am the lone developer using the framework so far) have not modified the CSLA code at all. I think maybe I might have screwed something up with importing the namespace or something?

This is a WinForms app, not webforms.

Mike

RockfordLhotka replied on Thursday, December 14, 2006

That's odd then - there shouldn't be anything called Csla.User...
 
I assume you are using a remote data portal configuration? If not, then the DataPortal_XYZ methods are running in the same AppDomain as all the rest of your code and would be using the exact same principal object. If you are using a remote data portal then you need to make sure both the client's app.config and server's web.config files have the same CSLA .NET authorization setting, otherwise the data portal won't properly impersonate the user across the network.
 
Rocky

Copyright (c) Marimer LLC