IIdentity casting problem

  • Thread starter Thread starter Craig Buchanan
  • Start date Start date
C

Craig Buchanan

I have an object, named SiteIdentity, that implements IIdentity. I have
added additional properties to this class, Email for instance. I am using
forms authentication.

In the code behind class for a webform, I try to cast from the HttpContext
identity object to my SiteIdentity object, but I get an error that reads
"Specified cast not valid". Here's the code:

If context.User.Identity.IsAuthenticated Then
Response.Write( Ctype( context.User.Identity,
MySite.Framework.Security.SiteIdentity).Email )
...

What am I missing?

Thanks,

Craig Buchanan
 
Hi,

have you assigned your custom principal/identity object to the
HttpContext.User on every request?

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist

I have an object, named SiteIdentity, that implements IIdentity. I have
added additional properties to this class, Email for instance. I am using
forms authentication.

In the code behind class for a webform, I try to cast from the HttpContext
identity object to my SiteIdentity object, but I get an error that reads
"Specified cast not valid". Here's the code:

If context.User.Identity.IsAuthenticated Then
Response.Write( Ctype( context.User.Identity,
MySite.Framework.Security.SiteIdentity).Email )
...

What am I missing?

Thanks,

Craig Buchanan
 
the error means that context.User.Identity is not a
MySite.Framework.Security.SiteIdentity. where is your code that sets
SiteIdenity to one of your objects.

-- bruce (sqlwork.com)
 
Teemu-

Does it need to be on *every* request? How about just on the
Global_AcquireRequestState method the of the global.asax?

Thanks,

Craig
 
bruce-

looks like i'm only doing this on an actual database authentication. i'm
not doing anything if it is a stored asp.net auth. cookie.

where should i be setting the siteidentity in this case?

craig
 
Craig Buchanan said:
bruce-

looks like i'm only doing this on an actual database authentication. i'm
not doing anything if it is a stored asp.net auth. cookie.

where should i be setting the siteidentity in this case?

In Application_AuthenticateRequest.
 
When the user logs in correctly the Principal object is stored in Session.

I use Global_AcquireRequestState to set the context to my custom Principal.

In my BasePage I cast the CurrentUser to my Principal.
Then all my properties are available in my custom Identity.
 
Back
Top