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
2
: 11753 At:- 6/23/2017 11:30:18 AM
C# asp.net-mvc browser forward-back-button







2 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

3
At:- 6/23/2017 1:37:11 PM
thanks 0
By : jaiprakash - at :- 11/20/2017 6:47:16 AM


profileImage Answered by:- jaya

You can also create a ActionFilter to Control Cache and disable back button after logout using the code below

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public sealed class CacheControlAttribute : ActionFilterAttribute
{
    private readonly HttpCacheability cacheability;
    public HttpCacheability Cacheability { get { return this.cacheability; } }
    public CacheControlAttribute(HttpCacheability cacheability)
    {
        this.cacheability = cacheability;
    }
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
        cache.SetCacheability(this.cacheability);
    }
}

And use it in your controller ActionMethod like

[CacheControl(HttpCacheability.NoCache)]
[HttpPost]
public ActionResult ActionName(){
//code
}

Or Another way, you can create a LogOut method like

//Call on clicking Logout button
public ActionResult LogOff()
{
  FormsService.SignOut();
//Clear session
  Session.Clear();
  Session.Abandon();
  Session.RemoveAll();
// Some More code
  return RedirectToAction("Index", "Home");
}
2
At:- 11/14/2017 3:21:58 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