1

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#?

James Monger
  • 10,181
  • 7
  • 62
  • 98
user2483797
  • 169
  • 1
  • 5
  • 13

3 Answers3

0

Create an if condition to check log in accounts. Then use the code in this reference to clear the cache if it satisfy the condition.

Community
  • 1
  • 1
Stuart
  • 711
  • 1
  • 11
  • 35
0

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

Clearing Page Cache in ASP.NET

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • This does not clear a browser cache at all. It sets caching policy for the page you are currently producing, something already in the browser cache will still be in the browser cache after it consumes the new web page. – Gary Walker Feb 28 '14 at 10:03
  • @GaryWalker I have updated my answer, please take a look now dude – Ramesh Rajendran Feb 28 '14 at 10:07
0

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

Gary Walker
  • 8,831
  • 3
  • 19
  • 41