Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable cache of browser in asp.net mvc 3?

I have created my custom authentication. Now I want to disable cache of broswer on log off button click. How should I do that? What should I include in log off action?

I'm following: http://www.bradygaster.com/custom-authentication-with-mvc-3.0

like image 707
kiransh Avatar asked Jul 20 '12 06:07

kiransh


People also ask

How do I disable caching in Visual Studio?

You can point cache dir to null using startup arguments for chrome. This will disable any caching.

What is the property that used to disable the cache storage in ASP NET MVC?

You can use the OutputCacheAttribute to control server and/or browser caching for specific actions or all actions in a controller.

How do I disable cache in web config?

If you want to disable caching of all files in a folder and its subfolders you can add a Web. config in the root folder you want to disable browser caching for. Say you wanted to disable browser caching for the files in the folder js/nocache and all subfolders, then you would place the Web. config here: js/nocache/Web.

How does MVC handle cache in asp net?

In ASP.NET MVC, there is an OutputCache filter attribute that you can apply and this is the same concept as output caching in web forms. The output cache enables you to cache the content returned by a controller action. Output caching basically allows you to store the output of a particular controller in the memory.


1 Answers

Is your concern the browser back button after logging off?

If yes, then you should not disable the cache on log off. You should disable it on all pages that you don't want to be cached which in your case would be all authentciated pages.

This could be done by writing a custom action filter:

public class NoCacheAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        var response = filterContext.HttpContext.Response;
        response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        response.Cache.SetValidUntilExpires(false);
        response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        response.Cache.SetCacheability(HttpCacheability.NoCache);
        response.Cache.SetNoStore();
    }
}

and then decorating your actions with it:

[Authorize]
[NoCache]
public ActionResult Foo()
{
    ...
}
like image 136
Darin Dimitrov Avatar answered Nov 15 '22 05:11

Darin Dimitrov