Urgent: Problem with custom 'Security.Principal.IPrincipal' implementation

From: Chris Lewis (chris.lewis@-nospam-exentra.no.spam.thanks.co.uk)
Date: 09/23/02


From: "Chris Lewis" <chris.lewis@-nospam-exentra.no.spam.thanks.co.uk>
Date: Mon, 23 Sep 2002 11:46:45 +0100


I have a very frustrating and urgent problem that seems to have arisen from
nowhere, and which I cannot figure out.

I am using Forms authentication, and have a non-secured root directory and a
'Secure' subdirectory (amongst others).

I have a custom implementation classes for the
'Security.Principal.IPrincipal' and 'Security.Principal.IIdentity'
interfaces - called SitePrincipal and SiteIdentity respectively.

The following code is from my 'Login' button event handler:

*** code ***
SitePrincipal newUser = m_userManager.AuthenticateUser(
UsernameTextBox.Text, PasswordTextBox.Text);
if (newUser == null)
{
 OutputLabel.Text = "Login failed for " + UsernameTextBox.Text;
 OutputLabel.Visible = true;
}
else
{
 Context.User = newUser;
 FormsAuthentication.SetAuthCookie( UsernameTextBox.Text, false);

 Response.Redirect(FormsAuthentication.GetRedirectUrl(UsernameTextBox.Text,
false));
}

*** end code ***

So I am setting Context.User = newUser, where newUser is an instance of
SitePrincipal - my custom implementation of IPrincipal.

The following is the code-behind from the target page of the redirect, and
is where the problem arises:

*** code ***
if (!Context.User.Identity.IsAuthenticated )
{
 // if not, redirect to the Login page
 Response.Redirect("blah.aspx", true);
}

if (!IsPostBack)
{
 SitePrincipal userPrincipal = (SitePrincipal)Context.User;
 SiteIdentity userIdentity = (SiteIdentity)userPrincipal.Identity;

 // Other stuff...
}
*** end code ***

The problem is that the object stored in Context.User is an instance of
'GenericPrincipal' and cannot be cast to SitePrincipal - so the error I get
is an 'InvalidCastException'!!!! Somewhere along the line, my SitePrincipal
has been down-cast to a GenericPrincipal...???!!!

I know that I have had this code working fine in the past! Please, if anyone
can help it would be much appreciated.

Chris Lewis


Quantcast