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?
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
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");
}
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly