p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

aspdotnet_website_programming thread: How does the cast: (SitePrincipal)Context.User work?


Message #1 by "Chris H" <chris@h...> on Thu, 6 Jun 2002 19:29:30
In the sample site, thePhile, thre are a few times where the 
HttpContext.User object s cast to a SitePrincipal. One example is line 28 
of the file ~/thephile/modules/users/admin/default.aspx.cs.  The 
expression is:

  ((SitePrincipal)Context.User).HasPermission(...)

My understanding of casting is that the above cast would only be valid if 
SitePrincipal is the base of IPrincipal, (i.e. I can't cast an object to 
something that knows more than the original). Am I wrong in this thinking?

The reason I'm asking is that I've made some modifications to the source 
of the sample and now get a "System.InvalidCastException: Specified cast
is not valid." error. 
I have not changed SitePrincipal.cs at all and can build the project OK in 
vs.net.  Neither thePhile nor my modified version works.  The only 
difference between the two (that I can tell) is that my version is not 
signed with a key file.

Has anybody else had this experience?

Thanks for the help and information.

--
Chris
Message #2 by "Mark Carranza" <markc@r...> on Sat, 8 Jun 2002 09:11:34
Chris, 

re: ~/thephile/modules/users/admin/default.aspx.cs.  

There's a bug in the code: if you go to this page without FIRST logging 
in, the SitePrincipal version of the Context.User object is never created, 
and never tested for.

Try logging in and then going to the page.  I leave it to you or the 
authors to fix this.  

IMPORTANT!!! BE SURE TO NOTICE in Default.aspx.cs, 
in the default hidden: Web Form Designer generated code:

<code>
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
 //
 // CODEGEN: This call is required by the ASP.NET Web Form Designer.
 //
 // *** REMEMBER TO SWITCH THE ORDER TO GET THE BASE FUNCTIONALITY -BEFORE-
 // *** THIS PAGE'S LOAD EVENT!
 base.OnInit(e);
 InitializeComponent();
}
</code>

THIS IS NOT (as far as I could find,) DOCUMENTED IN THE BOOK!!!

Of course my WebForms created in VS.NET were the default:

<code>
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
 //
 // CODEGEN: This call is required by the ASP.NET Web Form Designer.
 //
 InitializeComponent();
 base.OnInit(e);
}
</code>

... and getting a casting error because the page's Page_Load event is 
getting registered (delegated?) and fired BEFORE the 
foundation's "PhilePage_Load" event.  Grrr.  Days lost trying to figure 
this out.  Send money.
Message #3 by "Mark Carranza" <markc@r...> on Sat, 8 Jun 2002 09:53:45
One more thing!
Your pages MUST have:

<%@ Page ... AutoEventWireup="false" ... %>

...or else the page's Page_Load() wil be fired before the foundation's 
Page_Load().  

Can someone please tell me why?

  Return to Index