In the JavaScript file that I have included in the main .cshtml page of my ASP.NET MVC application, the following code can be found:
var ajax_call = function () {
$.ajax({
type: "GET",
cache: false,
url: "/Session/Index/",
success: function (result) {
if (!(result.length > 0)) {
window.location.href = '/Home/Index/'
}
}
});
};
var interval = 1000 * 60 * .2; // X minutes---.2 -> every 12 seconds
setInterval(ajax_call, interval);
The purpose is to verify the validity of the session (the SessionController only returns a result when the session is active). If the session is not valid, the browser is redirected to Home/Index, where HomeController has a global authorization attribute that redirects to the login page for invalid sessions.
My questions are: Is this approach appropriate? It appears to work as intended, but is there a way to achieve the same functionality entirely on the server side?
Thank you.