0

I am new to mvc application and i am using MVC 5 version.The scenario that i face now is like if i click browser back button on staying inside any webpage inside my application then login page should be shown instead of showing any cached page.

I added below code in global.asax to clear the cache.So after some time if cache is cleared and back button is clicked then expired page will be shown.

protected void Application_BeginRequest(){
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();
    }

My intention is to show the Login action method at any point of time if browser back button is clicked with in the aplication.Please help me out guys..

AnishK
  • 11
  • 4
  • Um, that's a client concern. Doing it on the server is not reliable. What happens when the cache is cleared and I click on a link? – beautifulcoder Jul 08 '15 at 15:44
  • possible duplicate of [Change browser back button behaviour for my web app?](http://stackoverflow.com/questions/9788688/change-browser-back-button-behaviour-for-my-web-app) – Liam Jul 08 '15 at 15:49
  • If link is clicked then corresponding action method will be called and will work fine.If browser back button is clicked i need to redirect to call login action method. – AnishK Jul 08 '15 at 15:51

1 Answers1

0

According to me this should never be done. The back button on browser is a client / user action and it has a specific behaviour. You should not try to change something like that.

But its all up to you if you want to do something that would be really bad according to user persceptive. What you can do is use the unload event in JavaScript or jquery. This will not just let you know when back button is pressed but when anytime the page is navigated away.

I had used this when dealing with data entry form to confirm navigation away after user has entered data without saving.

The unload event is sent to the window element when the user navigates away from the page. This could mean one of many things. The user could have clicked on a link to leave the page, or typed in a new URL in the address bar. The forward and back buttons will trigger the event. Closing the browser window will cause the event to be triggered. Even a page reload will first create an unload event.

http://api.jquery.com/unload/

$( window ).unload(function() { return "Handler for .unload() called."; });

I think now you would be thinking of distinguishing what causes the unload event. But that is not possible, at least not in a cross browser way and without any hacks. You can refer this link for more details about it - Is there a way in javascript to detect if the unload event is caused via a refresh, the back button, or closing the browser?

update (copied from another SO answer):

Since you are doing this for logout-purposes you should probably use a timestamp variable at the server that gets updated with every request (or use a ajax-ping), and logout the user if it hasn't been seen for a specified time.

Community
  • 1
  • 1
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112