Unable to cast object of type 'System.Security.Principal.GenericIdentity' ...

Unable to cast object of type 'System.Security.Principal.GenericIdentity' ...

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


tiago posted on Sunday, March 25, 2012

On my Web project I get this message when I'm not logged on.

System.InvalidCastException: Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'Project.Business.Security.CustomIdentity'.

This won't happen in production as users can't do anything at all until they login.

Strange enough, this doesn't happen when I fetch data, only when I try to update data.

BruderKotlett replied on Tuesday, November 27, 2012

Hi,

is there any solution for this problem? I have the same problem.

EDIT:

I have a CustomIdentity where I set the AuthenticationType to Custom. In my App.config (Client) I'll have this <add key="CslaAuthentication" value="Custom"/> and in my server config I'll have this <add key="CslaAuthentication" value="Custom"/>.

I use a CustomProxy which is also in the AppSettings section. Fetching data works, but no update. I saw in my CustomProxy, the DataPortalContext Principal is null. That's not right, or?

My CustomPrincipal und my CustomIdentity are both serializable.

[Serializable]

public class CustomPrincipal : Csla.Security.CslaPrincipal

-------

[Serializable]

public class CustomIdentity : CslaIdentityBase<CustomIdentity>

Kind regards

Kevin

RockfordLhotka replied on Tuesday, November 27, 2012

Are you using the ASP.NET Development Web Server? That server (code-named Cassini) has well-known limitations that block the use of custom principal types.

Use IIS Express instead, as it is more robust overall, and doesn't have the same limitations as Cassini.

BruderKotlett replied on Thursday, November 29, 2012

I'm using a WPF Application and WCF Services (with the DataPortalHost approach).

In my custom Proxy class I figured out, that the IsServerRemote property was false during GetProxy call from CSLA. So I set it to true in the GetProxy Method (_isServerRemote = true;) and than I'will have a valid Principal in the DataPortalContext. But in my ApplicationContext after logging in to my application, the Csla.ApplicationContext user was not authorized. So at least I changed the CustomProxy Update Method to this:

return GetProxy(obj.GetType()).Update(obj, new Csla.Server.DataPortalContext(Csla.ApplicationContext.User, true));

I'm using the CustomProxy for 3 different things: Local usage (return LocalProxy), WCF Server A and WCF Server B, because some business objects need another wcf service url (Server B) to store data.

This will work, but I don't know if this is the best way to do that.

Copyright (c) Marimer LLC