3

I am trying to restrict user to click back button after Login/Logout into the application.

If the user is logged in into the application, then after clicking on back button Login view should not be displayed and if user is logged out from the application then clicking on back button LogOff view should not be displayed.

I have already used caching technique which is given in number of links, but seems its not working.

This CacheAfterLogout link and done exactly as it is, still problem is not solved. I am using asp.net identity framework.

Could someone help on this ?

Community
  • 1
  • 1
XamDev
  • 3,377
  • 12
  • 58
  • 97

4 Answers4

4

To disable caching you can apply the following to each controller you wish to disable caching on

[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]

You can also setup named cache profiles and configure the settings at runtime.

blowdart
  • 55,577
  • 12
  • 114
  • 149
0

Cache can be disabled by applying the [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)] on each controller for which you wish to disable caching.

This can be applied to all controllers globally by adding your custom filter in startup.cs while configuring MVC services with ASP.Net Core 1.0.-* and MVC6.

First create a custom cache filter implementing the ResponseCacheAttribute as follows

public class ResponseCacheFilter : ResponseCacheAttribute
{
    public ResponseCacheFilter() : base()
    {
        Location = ResponseCacheLocation.None;
        NoStore = true;
    }
}

This should be added in startup.cs file as follows.

public void ConfigureServices(IServiceCollection services)
{
   // Add framework and custom services.
   services.AddMvc(config =>
     {
         config.Filters.Add(new ResponseCacheFilter());
     });
}

See the configuration of ResponseCache at https://docs.asp.net/en/latest/performance/caching/response.html#responsecache-attribute

-1

Just Add This Code in Global.asax.cs

protected void Application_BeginRequest()
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
        Response.Cache.SetNoStore();
    }
  • This solution looks identical to [this answer](https://stackoverflow.com/a/36838417/1575353) by Samyukta R.. If you haven't already, please read the comments from the OP and others. – Sᴀᴍ Onᴇᴌᴀ Sep 03 '17 at 05:45
-2

Just add these lines to your Global.asax.cs file.

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