Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After logout if browser back button press then it go back last screen

Tags:

Hello I am developing a solution in MVC in first time so I am facing a big issue, When I logout from my application(mvc razor web application) it displays login page, but if i press browser back button it displays last screen, i don't want this, i want if i press back button it still display same login page. here is my code for logout

public ActionResult Logout()     {         Session.Clear();         Session.Abandon();         Session.RemoveAll();          FormsAuthentication.SignOut();           this.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));         this.Response.Cache.SetCacheability(HttpCacheability.NoCache);         this.Response.Cache.SetNoStore();                    return RedirectToAction("Login");     } 
like image 682
Karan Prince Avatar asked Oct 11 '13 10:10

Karan Prince


People also ask

Why after logout clicking back button on the page displays previous page content?

It happens because your browser cached the page on the client. The solution is to prevent the caching of that page(s), by forcing the browser to request a new page even when pressing Back button, instead of reading the saved one.

How do I stop servlet from going back to previous page after logout?

put session attribute check(ex. user object) in restricted page controller. invalidate the session after logout.

How do I prevent someone from going back to previous page?

Master, I have used the code that to prevent the user from going back to previous pages after logout. function preventBack() { window. history. forward(); } setTimeout("preventBack()", 0); window.


2 Answers

I had this problem a while ago, disabling the cache for the entire application solved my problem, just add these line to the Global.asax.cs file

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

Hope this helps.

like image 113
AthibaN Avatar answered Sep 22 '22 15:09

AthibaN


You need to add the cache META Tag for all the last page you visited

So add this for all the pages, by making a CustomAttribute like [NoCache] and decorate

public class NoCacheAttribute : ActionFilterAttribute {       public override void OnResultExecuting(ResultExecutingContext filterContext)     {         filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));         filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);                     filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);         filterContext.HttpContext.Response.Cache.SetNoStore();          base.OnResultExecuting(filterContext);     } }   public class AccountController : Controller {     [NoCache]     public ActionResult Logout()     {         return View();     } } 

Or try it with javascript on the page like

<SCRIPT type="text/javascript">     window.history.forward();     function noBack() { window.history.forward(); } </SCRIPT>  <BODY onload="noBack();"     onpageshow="if (event.persisted) noBack();" onunload=""> 
like image 20
Murali Murugesan Avatar answered Sep 22 '22 15:09

Murali Murugesan