disable browser back-forward button after logout in MVC


how can I disable browser back-forward button after logging out from asp.net MVC C# web application, I have tried this javascript code

<script language="JavaScript" type="text/javascript">
//remove back button with all possibilities
     function noBack() { window.history.forward() }
     noBack();
     window.onload = noBack;
     window.onpageshow = function(evt) { if (evt.persisted) noBack() }
     window.onunload = function() { void (0) }
//remove forward buttons
    javascript:window.history.forward(1);
</script>

but I don't want to reply on javascript, is there in C# related code to remove this functionality?


Asked by:- jaiprakash
1
: 425 At:- 6/23/2017 11:30:18 AM
C# asp.net-mvc browser forward-back-button






1 Answers
profileImage Answered by:- vikas_jk

javascript method is not reliable, but it is still good for developers who don't want to code in backend like C#.

In C#, you can create an ActionFilter with the help of which, you can disable caching for any logged in page

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.SetRevalidation(HttpCacheRevalidation.AllCaches);
      filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
      filterContext.HttpContext.Response.Cache.SetNoStore();

      base.OnResultExecuting(filterContext);
  }
}

You can also place this HTML code on the pages in which you don't want browser caching

<meta Http-Equiv="Cache-Control" Content="no-cache">
<meta Http-Equiv="Pragma" Content="no-cache">
<meta Http-Equiv="Expires" Content="0">

 

if you are still not satisfied, check this link

1
At:- 6/23/2017 1:37:11 PM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use
Amazon Deals

Jobs from Indeed