Friday, 10 January 2014

Disable the view of page while click back button after Logout using custom action filter

In this article we are going to how to avoid the user to view the page while click the back button after log out, To do this we have to enable the clear the cache and expires the cache. So create the custom action filter to restrict the user to go for view the previous page.

Custom Action Filter Attribute:
public class NoCacheAttribute:ActionFilterAttribute
    {
      public override void  OnResultExecuting(ResultExecutingContext cntxt)
      {
            cntxt.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
     cntxt.HttpContext.Response.Cache.SetValidUntilExpires(false);                cntxt.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            cntxt.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            cntxt.HttpContext.Response.Cache.SetNoStore();
            base.OnResultExecuting(cntxt);
      }
    }



Action Filter Or Controller Filter


    [Authorize]  
    [NoCache]
    public class EmployeeController : Controller
    {
        Models.EmpModel mod = new Models.EmpModel();

        public ActionResult Create()
        {
            return View();
        }

       
        public ActionResult Index()
        {
            List<EMPTABLE> result = mod.GetEmployees();
            return View(result);
        }
   }



Output



After Logout if click the back button you can see that return url in concat in the url section



From this article we can learn how to disable the view of previous page after the logout.



No comments:

Post a Comment