Firewall configuration breaking ASP.NET AJAX? What are my options?

Last post 05-08-2008 11:55 AM by CitizenBane. 25 replies.

Sort Posts:

  • Firewall configuration breaking ASP.NET AJAX? What are my options?

    08-09-2007, 4:35 PM
    • Member
      1 point Member
    • alioop8080
    • Member since 08-09-2007, 8:00 PM
    • Posts 2

    Hi There,

    I just switched my code to use Update Panels last week and now a small number of customers are complaining about a Sys.WebForms.PageRequestManagerParserErrorExeption error. It is the not the usual issue with Response.Write(), response filters, HttpModules, or server trace is enabled. The pages work for most users and we cannot reproduce the error.

    This posting about firewall configuration seems to be accurate (http://blogs.telerik.com/blogs/twisted_asp_net/archive/2007/06/26/2528.aspx) and when I asked a user with the issue to go to a simple Update Panel tutorial page written by Microsoft, he saw the same error message.

    Basically the update panel is injecting "X-MicrosoftAjax" into the request headers and the users' firewalls are configured to block unknown headers and is stripping it out so the Update Panel doesn't know what to do with the response.

     We can't really ask all these customers to change their firewall configurations, as the link above suggests, so I'm wondering if anyone has ideas about how I could detect this issue and then force a postback in these cases? Or any other more user-friendly alternatives?

     
    Any suggestions are welcome. Thanks.

    _ali
     

  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    08-09-2007, 5:54 PM
    Answer
    • All-Star
      21,648 point All-Star
    • gunteman
    • Member since 07-11-2007, 8:57 AM
    • Norrköping, Sweden
    • Posts 3,177

    I had this problem some time ago, and I think I resolved it.

     In BeginRequest (Global.asax or in an HttpHandler).....

     

    HttpRequest request = HttpContext.Current.Request;
    if (request.Headers["X-MicrosoftAjax"] == null && request.Form["__MicrosoftAjax"] != null)
    {
       request.Headers.GetType().InvokeMember("MakeReadWrite", System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, null, request.Headers, null);
       request.Headers.Add("X-MicrosoftAjax", request.Form["__MicrosoftAjax"]);
       request.Headers.GetType().InvokeMember("MakeReadOnly", System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, null, request.Headers, null);
    }

    and on the page with the UpdatePanel...

     

    <script type="text/javascript>
    function beginRequest(sender, args) {
       var r=args.get_request();
       if (r.get_headers()["X-MicrosoftAjax"])
       {
        b=r.get_body();
        var a="__MicrosoftAjax=" + encodeURIComponent(r.get_headers()["X-MicrosoftAjax"]);
        if (b!=null && b.length>0)
        {
    	b+="&";
        }
        else
    	b="";
        r.set_body(b+a);
       }       
    }
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
    </script type="text/javascript>
    The idea is to intercept the async postback on the client and copy the value from the X-header into the regular POST data. On the server the request is also intercepted and if the post field is present and the header is not (indicating that it was removed by a firewall) , we add the header to the request. Since the headers collection is read-only some reflection magic is needed to open it up.
    -- "Mark As Answer" if my reply helped you --
  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    08-10-2007, 1:34 PM
    • Member
      1 point Member
    • alioop8080
    • Member since 08-09-2007, 8:00 PM
    • Posts 2
    Thank you! This worked really well.
  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    09-27-2007, 11:23 AM

    Hi,

    I am having the same problem, (how come more people haven't come forward, and are MS addressing this issue?).

    However, using your fix I am now getting the error: "Operation is not supported on this platform." at System.Web.HttpHeaderCollection.Add(String name, String
    value) - during the async postback through a firewall.

    The server is running Windows Server 2003, ASP.NET 2.0 C#, and everything's fully patched.

     Am I missing something?

    Kind regards

    Chris

    Chris Chamberlain
    Head of IT
    VEF (UK) Ltd

    Visit VEF French Property
  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    09-27-2007, 2:39 PM
    • All-Star
      21,648 point All-Star
    • gunteman
    • Member since 07-11-2007, 8:57 AM
    • Norrköping, Sweden
    • Posts 3,177

    I think it depends a bit on the environment if it works or not. You can try this alternative version:

     

    HttpRequest request = HttpContext.Current.Request;
    if (request.Headers["X-MicrosoftAjax"] == null && request.Form["__MicrosoftAjax"] != null)
    {
    	ArrayList list = new ArrayList();
    	list.Add(request.Form["__MicrosoftAjax"]);
    	Type t = request.Headers.GetType();
    	lock (request.Headers)
    	{
    	    t.InvokeMember("MakeReadWrite", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, request.Headers, null);
    	    t.InvokeMember("InvalidateCachedArrays", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, request.Headers, null);
    	    t.InvokeMember("BaseSet", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, request.Headers, new object[] { "X-MicrosoftAjax", list });
    	    t.InvokeMember("MakeReadOnly", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, request.Headers, null);
    	}
    }
    I haven't tried it, but I guess it could work.  
    -- "Mark As Answer" if my reply helped you --
  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    10-01-2007, 7:22 AM

    Thank you. This has made the property search work perfectly.

    The departments of France dropdown now changes when a region is selected and the image for the map also changes.

    Kind regards

    Chris Chamberlain
    Head of IT
    VEF (UK) Ltd

    Visit VEF to find property in france.

    Chris Chamberlain
    Head of IT
    VEF (UK) Ltd

    Visit VEF French Property
  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    11-02-2007, 4:01 PM

    I convert the code to VB.net. But it doesn't work. I still get the same error message as before. I did not change the javascript function and just copy and paste it to the <head> section of the page have update pannel. Please help!!

     Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)

     

          Dim request As HttpRequest = HttpContext.Current.Request

          If (request.Headers("X-MicrosoftAjax") = Nothing) And (Not request.Form("__MicrosoftAjax") = Nothing) Then

               Dim list As ArrayList = New ArrayList()

                list.Add(request.Form("__MicrosoftAjax"))

               Dim t As Type = request.Headers.GetType()

               Lock(request.Headers)

               t.InvokeMember("MakeReadWrite", System.Reflection.BindingFlags.InvokeMethod Or System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance, Nothing, request.Headers, Nothing)

               t.InvokeMember("InvalidateCachedArrays", System.Reflection.BindingFlags.InvokeMethod Or System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance, Nothing, request.Headers, Nothing)

               t.InvokeMember("BaseSet", System.Reflection.BindingFlags.InvokeMethod Or System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance, Nothing, request.Headers, New Object() {"X-MicrosoftAjax", list})          t.InvokeMember("MakeReadOnly", System.Reflection.BindingFlags.InvokeMethod Or System.Reflection.BindingFlags.NonPublic Or     System.Reflection.BindingFlags.Instance, Nothing, request.Headers, Nothing)

     

       End If

     

    End Sub

  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    11-02-2007, 6:20 PM
    • All-Star
      21,648 point All-Star
    • gunteman
    • Member since 07-11-2007, 8:57 AM
    • Norrköping, Sweden
    • Posts 3,177

    The lock clause should be written as

    SyncLock request.Headers
        ...
    End SyncLock
          

     

    -- "Mark As Answer" if my reply helped you --
  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    11-03-2007, 9:58 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368

    hello.

    nice workaround, but i think you should use a private object for locking...

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    11-03-2007, 11:14 AM
    • All-Star
      21,648 point All-Star
    • gunteman
    • Member since 07-11-2007, 8:57 AM
    • Norrköping, Sweden
    • Posts 3,177

    Absolutely. Good point. Actually I think the lock should be removed completely.

    -- "Mark As Answer" if my reply helped you --
  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    11-03-2007, 11:16 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368

    hello again.

    yeah, agreed...

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    11-04-2007, 9:34 PM

    I remove the lock from code. But it still doesn't work. Same error message as before.

  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    11-05-2007, 4:42 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368

    hello.

    have you:

    1. debugged tghe code and made sure the code you've written is executed?

    2. used fiddler to see what you're getting frmo the database?

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    11-05-2007, 9:39 AM

    Hi, I debug the code inside the firewall and find that:

    request.Headers("X-MicrosoftAjax")="Delta=true"

    request.Form("__MicrosoftAjax")=Nothing.

    Is that because "request.Form("__MicrosoftAjax")=Nothing" causes the problem? I just copy and paste the javascipt code to the page in which the update panel is located. Is it because something I did is  wrong? Thanks.

  • Re: Firewall configuration breaking ASP.NET AJAX? What are my options?

    11-05-2007, 9:42 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368

    hello again.

    well, but the previous code is no JS code. it's server side code that is supposed to be run on your asp.net server side.

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
Page 1 of 2 (26 items) 1 2 Next >