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

僤鯓⒐⒋嵵緔 提交于 2019-12-24 10:39:15

问题


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.


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



回答2:


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




回答3:


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>



回答4:


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>


来源:https://stackoverflow.com/questions/7597854/how-to-handle-exception-due-to-expired-authentication-ticket-using-updatepanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!