For my application I want to clear the cache before logging. However, if someone logs with different account, I want to clear the cache so that the page is refreshed and doesn't keep the previous user's values.
How do I do this in C#?
For my application I want to clear the cache before logging. However, if someone logs with different account, I want to clear the cache so that the page is refreshed and doesn't keep the previous user's values.
How do I do this in C#?
Using this Code you can clear the browser cache.
You can also use it on LogOut
of your site instead of login
.
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Or you can do this way
https://stackoverflow.com/a/2876701/2218635
You can define a cache by this way
HttpContextBase httpContext = filterContext.HttpContext; httpContext.Response.AddCacheItemDependency("Pages");
and setup a cache when login
protected void Application_Start()
{
HttpRuntime.Cache.Insert("Pages", DateTime.Now);
}
and clear cache when logout
HttpRuntime.Cache.Insert("Pages", DateTime.Now);
more details
You cannot write code to clear the client browser cache. All you can do it set control cache policy for data when you send the date to the browser in the first place.
E.g., if you initially have an image file, with expiration set to midnight when the browser retrieves it, the browser will not remove the file from its cache before midnight. If you need to immediate force the browser to fetch a different version of the file, the URI must change -- i.e., rename the file to a new (version 2) name.
You can only control cache policy of item sent to the browser (or the intervening proxy server, or both). The browser can ignore the policy (if it wants to be a really bad browser), but you CAN NOT send anything that will clear pre-existing browser cache.
Policy (in the http header), set things like expiration time (GMT), relative time (i.e., cache duraction), as well as no-cache directives. You can set this for the browser cache (or proxy and shared cache). But once it goes over the wire, you can not clear it.
ADDED
Took me a while to find this article explaining how browser caching works. Easier to understand than the W3C explanation of browser caching