Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 26th, 2007, 04:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default 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

  #2 (permalink)  
Old December 29th, 2007, 09:51 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
  #3 (permalink)  
Old December 29th, 2007, 08:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

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

  #4 (permalink)  
Old December 30th, 2007, 04:46 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
  #5 (permalink)  
Old December 30th, 2007, 11:27 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

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

  #6 (permalink)  
Old December 31st, 2007, 01:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

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

  #7 (permalink)  
Old December 31st, 2007, 01:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

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.



  #8 (permalink)  
Old December 31st, 2007, 06:05 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wire-up Validator controls cbigden BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 0 September 25th, 2007 10:28 AM
Javascript Errors Not Appearing itHighway Javascript How-To 1 October 13th, 2005 01:30 AM
DataGrid Controls Within custom validator control geetha_ganesan ADO.NET 1 March 26th, 2005 04:38 PM



All times are GMT -4. The time now is 12:39 AM.


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