开发者

How to handle exception due to expired authentication ticket using UpdatePanel?

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: the开发者_运维问答re 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.


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");


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


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>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜