1

My MVC Application gets timeout after 20min. ie, When I click any buttons after 20mins automatically the application will get redirected to login page. All the buttons in my page uses Ajax call, hence when we click on any button the ajax will return failure code and redirected to login page.

Problem: If I am in the middle of a page with some search criteria and paging, when I login again after timeout I am not able to come to the exact page where I was before timeout. As I am using Ajax calls on button clicks I am not able to see the search parameters or page numbers on Url. Please can someone help on this.

On Timeout Ajax is returning the below error status and redirected to login page.

  error: function (xhr, ajaxOptions, thrownError) {
                   if (xhr.status == 401) {
                       window.location.href = "/Home/Logout";
                       return;
                   }

           }
VVR147493
  • 251
  • 2
  • 4
  • 16

2 Answers2

2

You can use the history API to keep track of the exact url where you are. You do have to map it back in your routes / controllers to the desired page.

Check out history.js which makes life easier:

You have to call this API when clicking on any link / button that causes AJAX calls which you want to 'remember'.

Sample from SO:

function search(params) {
  // record your current UI position
  // data (to save), title (to set on page), url (to set on page)
  History.pushState({ params: params }, "Search", "?search");

   // now run whatever should happen because client triggered search()
}

You have to update the url at every action. Then, at the Logoff action, add the current url as ReturnUrl parameter in your Logoff url and it will work.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thanks Patrick. How I can use history.js file in my case. ie, after timeout and again when I login back to navigate to exact page. – VVR147493 Feb 18 '14 at 14:30
0

Since you use ajax calls to perform search instead of URL, you could use cookies together with history.js to repeat the last search before logout.

In your search button click JS handler:

function search(params) {
  // record your current UI position
  // data (to save), title (to set on page), url (to set on page)
  History.pushState({ params: params }, "Search", "?search");

   // Ajax call here to do your search
}

Create a cookie in your Login action before the redirect:

Response.Cookies.Add(new HttpCookie("UserLogin"));

Add a document.ready JS function to check if "UserLogin" cookie exists, if it does then

if (cookieExists("UserLogin")){
  removeCookie("UserLogin");
  var State = History.getState();

  if (State.url == "?search") {
    search(data.params);
  }
}

(not tested)

artm
  • 8,554
  • 3
  • 26
  • 43