1

I am pretty sure the reason of the error is because the forms authentication "ticket" has expired. When the users have not done any pagerequest for the last 20 minutes and click on any of GridView links (to edit, delete, sort...) the exception is raised: Sys.WebForms.PageRequestManagerServerErrorException 12031.

The exception is only raised when the GridView is inside an UpdatePanel.

If I delete the UpdatePanel, the application redirects the user to the login page, which should be the expected behaviour.

How can I catch this exception, in order to redirect the user to the login page?

Note: there is already a question about the same error: Sys.WebForms.PageRequestManagerServerErrorException 12031. However, the reason is different since it is related to the size of the objects stored in the ViewState, which is not my case.

Community
  • 1
  • 1
aleafonso
  • 2,244
  • 8
  • 38
  • 58
  • 1
    So far, there have been 63 views, only 1 answer, and not even 1 upvote? Come on guys, let's collaborate with each other. If you don't know the answer to the question, at least, help me out with a simple upvote. Especially since this could be a common problem. Don't you agree? – aleafonso Oct 05 '11 at 10:56
  • SO can be a tough crowd. – Brett Oct 05 '11 at 15:00
  • Can't you check for login in whatever is the first method in the page-life-sycle? Or if that's to early, at least you should be able to check when session or viewstate is loaded. – Alxandr Oct 06 '11 at 04:52
  • Hi Alxandr. I am not sure if this could solve the issue since the Update Panel usage "skips" the initial steps of the normal page life cycle. The user might be logged in when you loaded the page, but after the ticket has expired the user could still click on the GridView's links where the error is fired. – aleafonso Oct 06 '11 at 07:50
  • The solution was given in the following thread: http://stackoverflow.com/questions/7586469/how-can-i-handle-forms-authentication-timeout-exceptions-in-asp-net – aleafonso Oct 28 '11 at 10:52

4 Answers4

1

Add a Global.asax (if you don't have one).

protected void Application_Error(object sender, EventArgs e)
{
    // Get the last exception
    Exception ex = Server.GetLastError();
...

and if the exception is a PageRequestManagerServerErrorException

Server.ClearError();
Response.Redirect("~/login");
Michael Olesen
  • 473
  • 2
  • 9
  • Hi Michael. Thanks for your response. However, you'd agree with me if I say this handles every kind of exception instead of handling this particular one. I was thinking more about an implementation where I could avoid the exception to occur, or handle it during the webform life cycle. Do you think that it is possible? Thanks for sharing – aleafonso Oct 04 '11 at 09:54
  • Yes it will catch all. 12031 means ERROR_INTERNET_CONNECTION_RESET (http://support.microsoft.com/kb/193625). Maybe you could check for "logged-in/connection-reset" in an event on gridview or updatepanel that get executed, when you edit/delete/sort – Michael Olesen Oct 04 '11 at 15:35
0

Server-side you can handle this exception from the AsyncPostBackError event of your UpdatePanel. This will allow you for example to log the error.

To redirect you need to handle the exception client-side to customize the error handling (and redirect to login in your case).

Both are documented here: http://msdn.microsoft.com/en-us/library/bb398934.aspx

teebot
  • 1,089
  • 2
  • 12
  • 25
  • Hi teebot.be. I appreciate your answer. However, I have just tried and it does not work for this particular situation. First of all, AsyncPostBackError is a ScriptManager's event, not an UpdatePanel's. Therefore, I try to catch the error in my MasterPage (where I have the ScriptManager). However, it breaks before it gets to the event handler (ScriptManager1_AsyncPostBackError). Any other idea will be more than welcome. – aleafonso Oct 05 '11 at 16:18
0

If you are getting a response to the browser with the exception then you can catch it by wiring up the endRequest event of the ScriptManager and checking for the presence of an error and the proper httpStatusCode. Just be sure to add the javascript below the asp:ScriptManager tag so the browser will recognize the namespace.

If you need to extend this further check out the MSDN documentation

<script type="text/javascript" language="javascript">
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

    function EndRequestHandler(sender, args)
    {
        // Verify the httpStatusCode you are receiving
        if (args.get_error() != undefined && args.get_error().httpStatusCode == '302')
        {
            args.set_errorHandled(true);
            alert('Authentication expired, redirecting to login page');
            location.href='login.aspx'; // Whatever your login page is
        }
    }
</script>
Brian
  • 68
  • 1
  • 6
0

I don't know why it happens, but when it does, I just mark the error as handled and nothing blows up. Just add the following JavaScript and you're troubles will disappear.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <script type="text/javascript" >
        (function() {
            var prm = Sys.WebForms.PageRequestManager.getInstance();

            if (prm)
            {
                prm.add_endRequest(
                function (sender, args) {            
                    // Any code you want here

                    if(args.get_error() && args.get_error().name === 'Sys.WebForms.PageRequestManagerServerErrorException')
                    {
                        args.set_errorHandled(args._error.httpStatusCode == 0);
                    }
                });
            }
        })();
        </script>
    </form>
</body>
</html>
sikender
  • 5,883
  • 7
  • 42
  • 80