0

We are using datatables with ASP.Net MVC and identity framework. I have set the authentication timeout to 1 minute using the code below:

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        ...

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            ExpireTimeSpan = System.TimeSpan.FromMinutes(1),
            Provider = new CookieAuthenticationProvider
            {
                ...
        });

I then login and go to a page with a datatable and wait for the timeout. An error occurs if the timeout expires and the datatable tries to hit the server. The datatable works by making an ajax request to the server and this is where the error comes from.

The error is a JavaScript error:

DataTables warning: table id=DataTables_Table_0 - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

I need to handle this gracefully and redirect the user to the login page.

Can anyone help please?

Burt
  • 7,680
  • 18
  • 71
  • 127

2 Answers2

1

.NET has a built in Time Out Exception:

try 
    {
        // your logic here that might time out
    }
catch (TimeoutException e)
    {
       // your logic here if it does time out
    }

More: https://msdn.microsoft.com/en-us/library/system.timeoutexception(v=vs.110).aspx

EDIT:

For an AJAX time out, maybe this thread would help?

Handling session timeout in ajax calls

Community
  • 1
  • 1
Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
  • It is not really a timeout exception Casey. It is an Ajax request that fails due to the authentication cookie expiring and requests being refused due to the authorize attribute on the action method. – Burt Jun 10 '15 at 16:10
1

You can always use the Application_Error event handler in the Global.asax file. Add the:

protected void Application_Error(object sender, EventArgs e)
{
    //Redirect code here
}

method, and load in your code to check if the login is valid. If not, you can route them to another page. In one of my projects a while back I used the RouteData class to send them to another page:

//send to the error page
var routeData = new RouteData();
routeData.Values.Add("controller", "Error");

if (!userAuthorized)
    routeData.Values.Add("action", "NotAuthorized");
else
    routeData.Values.Add("action", "Index");

routeData.Values.Add("message", exception.Message);

IController errorController = new DevWebApp.Controllers.ErrorController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
jwatts1980
  • 7,254
  • 2
  • 28
  • 44
  • 1
    @Burt I don't think so. Most likely you'll have to use a `try/catch` block to capture the error. In the catch, you can check if the user is authorized, and if not, you'll need to send something back through the AJAX return value that indicates an "Unauthorized" error has occurred. You can then use JavaScript to redirect the page to the login screen. – jwatts1980 Jun 10 '15 at 16:12
  • 1
    @Burt I apologize, I did not see anything in your question that indicated it was an AJAX call. – jwatts1980 Jun 10 '15 at 16:13
  • 1
    @Burt It is good to make sure that you design return data from an AJAX call so that it has a way to pass error messages back to the requesting page. – jwatts1980 Jun 10 '15 at 16:15
  • Updated the question Jason, sorry about the confusion, I assumed anyone answering would be aware of the datatable ajax call, my bad. – Burt Jun 10 '15 at 16:16
  • You got any examples? – Burt Jun 10 '15 at 16:16
  • @Burt You said that an error occurs. Is it a JavaScript error? – jwatts1980 Jun 10 '15 at 16:26
  • It comes from the datatable, I can't track the error. That is the tricky bit. I can't get a hook on it. – Burt Jun 10 '15 at 16:29
  • @Burt This a web application right? The error is either a server error (which I assume if you are using Visual Studio and are running the local server) or it is a JavaScript error that is showing up in the browser console. Is the error on the server side or the client side? Or, how do you know that an error has occurred? – jwatts1980 Jun 10 '15 at 16:32
  • @Burt I have not used .NET's DataTable, but I've used Telerik's Kendo UI Grid, which is pretty awesome. Can you elaborate, in your question, on what the client side JavaScript looks like? For these AJAX calls, is this JavaScript code that you are having to write? Or is it something that the control is handling automatically? – jwatts1980 Jun 10 '15 at 19:53
  • @Burt Also, what does the server side processing look like? – jwatts1980 Jun 10 '15 at 20:04