Invalid Cast Exception with Custom Identity

Invalid Cast Exception with Custom Identity

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


JabbaTheNut posted on Wednesday, May 09, 2007

I have custom identity and principal objects (LVIdentity and LVPrincipal) that are identical to PTIdentity and PTPrincipal except that I have added an ID property to LVIdentity.  Upon login, I set the Csla.ApplicationContext.User to my custom principal in the following way:

LVIdentity identity = LVIdentity.GetIdentity(username, password);

LVPrincipal principal = new LVPrincipal(identity);

Csla.ApplicationContext.User = principal;

I have read in other posts that in order to access the ID property of the Csla.ApplicationContext.User.Identity object, I must cast it as my custom LVIdentity:

LVIdentity identity = (LVIdentity)Csla.ApplicationContext.User.Identity;

int m_id = identity.ID;

As confirmation of this, I am unable to see the ID property in Intellisense until I cast in the above manner.

The problem I am having is that when I attempt to use the above code, I get the following error:

"DataPortal.Fetch failed: unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'LV.Security.LVIdentity'".

What am I missing?

RockfordLhotka replied on Wednesday, May 09, 2007

Before you call the data portal, even the first time (like to log in), you need to have a "valid" principal that inherits from the CSLA base class. Odds are you have left it as the default as your app starts - which would be a genericprincipal.

JabbaTheNut replied on Thursday, May 10, 2007

Rocky,

You're the best!  That was it.  Thanks :)

Copyright (c) Marimer LLC