p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Professional (http://p2p.wrox.com/forumdisplay.php?f=215)
-   -   Validator controls throwing JavaScript errors (http://p2p.wrox.com/showthread.php?t=64808)

Bob Bedell December 26th, 2007 04:29 AM

Validator controls throwing JavaScript errors
 
Pardon the posting binge but...

My validation controls in 2.0 are throwing a 'WebForm_PostBackOptions is undefined' error.

A web search turned up plenty of folks with the same issue, but no fixes I can get to work.

http://blogs.msdn.com/paraga/archive...02/522766.aspx

The link above suggests commenting out the AcquireRequestState event in global.asax, but I just so happen to need that event.

http://www.inventua.com/blogs.content?EntryID=5

This link suggests copying a legitimate WebResource.axd file (the new JavaScript function file in 2.0) to my web app directory. Tried it, no luck.

Anyone run into this and had success with a fix.

Best,

Bob


Imar December 29th, 2007 09:51 AM

Hi Bob,

Did you recently install Visual Studio 2008? I know there were some issues with VS2008 and ASP.NET 2 validation controls, and you had to run some script to fix the problem. Not sure if it's related though....

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.

Bob Bedell December 29th, 2007 08:57 PM

Hi Imar,

Definitely a quirky problem that appears to have a variety of manifestations.

The problem is caused by a session state acquisition conflict between the handlers in Global.asax (AcquireRequestState in my case) and WebResource.axd. In ASP.NET 2.0, WebResource.axd is used internally by the .NET Framework to retrieve embedded resources, such as the WebUIValidation.js script file that is used for Validation controls and Menu.js script file for Menu controls. My problem involved being unable to load WebUIValidation.js.

Clear explanations of the issue aren't easy to come by, but after three days of googling I hit this which pointed me toward a fix:

http://jeffbergman.com/cs/blogs/csje.../07/27/10.aspx

I finally worked the issue out today with someone on the CSLA forum:

http://forums.lhotka.net/forums/thread/20057.aspx

My original AcquireRequestState event looked like this, and prevented the request to WebResource.axd from suceeding:

Code:

protected void Application_AcquireRequestState(Object sender, EventArgs e)
    {

         if (Session["CSLA-Principal"] != null)
         {
            System.Threading.Thread.CurrentPrincipal = (System.Security.Principal.IPrincipal)Session["CSLA-Principal"];
            HttpContext.Current.User = System.Threading.Thread.CurrentPrincipal;
        }
        else
        {
            if (System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
            {
                System.Web.Security.FormsAuthentication.SignOut();
                Server.Transfer("Login.aspx");
            }
        }
    }

With the following version, wrapped with a session null check, WebResource.axd loads WebUIValidation.js sucessfully, and my validation controls work as advertised:

Code:

protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        if (HttpContext.Current.Session == null)
        {
            //do nothing
        }
        else
        {
            if (Session["CSLA-Principal"] != null)
            {
                System.Threading.Thread.CurrentPrincipal = (System.Security.Principal.IPrincipal)Session["CSLA-Principal"];
                HttpContext.Current.User = System.Threading.Thread.CurrentPrincipal;
            }
            else
            {
                if (System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
                {
                    System.Web.Security.FormsAuthentication.SignOut();
                    Server.Transfer("Login.aspx");
                }
                else
                {
                    System.Threading.Thread.CurrentPrincipal = (System.Security.Principal.IPrincipal)Session["CSLA-Principal"];
                    HttpContext.Current.User = System.Threading.Thread.CurrentPrincipal;
                }
            }
        }
    }



Anyway, all is well, all issues resolved. Hope you had some good R & R.

Best,

Bob


Imar December 30th, 2007 04:46 AM

Yes, I did. Thank you. Hope you had a good time too....

Interesting situation; thanks for posting a follow up. There's one minor thing in your code I noticed:
Code:

protected void Application_AcquireRequestState(object sender, EventArgs e)
  {
       if (HttpContext.Current.Session == null)
       {
           //do nothing
       }
       else
       {
           if (Session["CSLA-Principal"] != null)
           {
             // Not null
           }
           else
           {
              // Is Null
               if (System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
               {
                  //
               }
               else
               {
                  // This will crash, right?
                  // Session["CSLA-Principal"] IS null....
                   System.Threading.Thread.CurrentPrincipal = (System.Security.Principal.IPrincipal)Session["CSLA-Principal"];
               }
           }
       }
   }

   Looks like you can end up in a situation where Session["CSLA-Principal"] is NULL, yet you try to access it.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.

Bob Bedell December 30th, 2007 11:27 PM

Hi Imar,

Yup, I enjoyed the break too. Got the snot beat out of me by my 6 year old nephew for several days. Good to see the folks and bro. All is well.

Session["CSLA-Principal"] actually should be null until after the user authenticates. Here the null check is for a session variable that indexes the SessionStateItemCollection, not for the existence of the Session object itself. If session doesn’t exist, this line can’t be reached. ["CSLA-Principal"] indexes a CSLA.Security.BusinessPrincipal that is manually added to session after the user authenticates.

Here' the blow by blow:

Code:

protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        if (HttpContext.Current.Session == null)
        {
            // do nothing - Session object not
            // instantiated yet.
        }
        else
        {
            // The Session object has been instantiated or the first null
            // check would have returned 'true'. Here I'm just indexing the
            // SessionStateItemCollection for a session-state variable.
            // Indexing the SessionStateItemCollection simply returns null if
            // the requested session variable doesn’t exist. The "CSLA-Principal"
            // session variable won't exist (will be null) until after the user
            // has authenticated and a custom CSLA.Security.BusinessPrincipal
            // object has been manually added to session-state by a method of
            // the login form.
            if (Session["CSLA-Principal"] != null)
            {
                // The user has already authenticated and the CSLA.Security.BusinessPrincipal
                // object stored in session-state is assigned as the users CurrentPrincipal. 
                System.Threading.Thread.CurrentPrincipal = (System.Security.Principal.IPrincipal)Session["CSLA-Principal"];
                HttpContext.Current.User = System.Threading.Thread.CurrentPrincipal;
            }
            else
            { 
                // Fires if user logs out later in the session.
                if (System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
                {
                    System.Web.Security.FormsAuthentication.SignOut();
                    Server.Transfer("Login.aspx");
                }
                else
                {  // Unauthentiucated user assigned a GenericPrincipal object until
                    // they authenticate.
                    System.Threading.Thread.CurrentPrincipal = (System.Security.Principal.IPrincipal)Session["CSLA-Principal"];
                    HttpContext.Current.User = System.Threading.Thread.CurrentPrincipal;
                }
            }
        }
    }



For completeness sake, here's the login button click event:
Code:

protected void btnLogin_Click(object sender, System.EventArgs e)
    {
        string userName = txtUsername.Text;
        string password = txtPassword.Text;

        // If logging in, clear the current session.
        Session.Clear();

        // Log into the system. BusinessPrinciple methods
        // query a security database, load an Idendity object,
        // and set the threads current principle the the
        // BusinessPrincipal, which now references either an
        // authentucated or unauthenticated Identity object,
        // based on the query results.
        BusinessPrincipal.Login(userName, password);

        // see if we logged in successfully
        if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
        {
            // The authenticated CSLA.Security.BusinessPrincipal
            // gets added to session-state as a session variable named
            // "CSLA-Principal".
            Session["CSLA-Principal"] = Thread.CurrentPrincipal;
            HttpContext.Current.User = Thread.CurrentPrincipal;

            // redirect to the page the user requested
            FormsAuthentication.RedirectFromLoginPage(
                userName, false);
        }
    }



Best,

Bob


Bob Bedell December 31st, 2007 01:25 AM

Geeez, after all that I finally read your code carefully enough to see you're actually concerned about:

// This will crash, right?
// Session["CSLA-Principal"] IS null....
System.Threading.Thread.CurrentPrincipal = (System.Security.Principal.IPrincipal)Session["CSLA-Principal"];

For some reason I just started writing about your first "//Not null"
notation. My apologies for the session variables 101 lecture.

When Session["CSLA-Principal"] is null, and is assigned to CurrentPrincipal, .NET automatically instantiates a GenericPrinciple, which CurrentPrinciple then references. So no, it doesn't crash. The app just works with a GenericPrinciple until Session["CSLA-Principal"] references an authenticated custom BusinessPrincipal.

Best,

Bob


Bob Bedell December 31st, 2007 01:39 AM

I guess it would be more accurate to say that the GenericPrinciple is created at startup, it just isn't replaced with a null reference.

Here's the values of the two expression immediately after

System.Threading.Thread.CurrentPrincipal = (System.Security.Principal.IPrincipal)Session["CSLA-Principal"];

executes (from the Immediate Window):

?(System.Security.Principal.IPrincipal)Session["CSLA-Principal"];
null

?System.Threading.Thread.CurrentPrincipal
{System.Security.Principal.GenericPrincipal}
    [System.Security.Principal.GenericPrincipal]: {System.Security.Principal.GenericPrincipal}
    Identity: {System.Security.Principal.GenericIdentity}

The assignment just doesn't have any affect on the original value of CurrentPrincipal.




Imar December 31st, 2007 06:05 AM

Haha, don't worry about it....

I just thought that this:

System.Threading.Thread.CurrentPrincipal = (System.Security.Principal.IPrincipal)Session["CSLA-Principal"];

would cause an exception when Session["CSLA-Principal"] is null. However, apparently it's OK to cast null to a System.Security.Principal.IPrincipal and assign it. Funny thing is indeed that after the null assignment, System.Threading.Thread.CurrentPrincipal is still a valid object, so it seems the assignment is either ignored or reset....

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.


All times are GMT -4. The time now is 07:08 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.