Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear browser cache when user log off in asp.net using c#?

As new in asp.net. In my asp.net application in membership in log off on click event using function ClearSession(), but problem arises after log off if i click back button on browser it is forwarding to the cached page. How to clear cache in browser so a user could not view its profile if he is not login

protected void ClearSession()
{
    FormsAuthentication.SignOut();
    Session.Clear();
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ExpiresAbsolute = DateTime.UtcNow.AddDays(-1d);
    Response.Expires = -1500;
    Response.CacheControl = "no-Cache";
}
like image 734
Hassaan Avatar asked Mar 10 '14 16:03

Hassaan


People also ask

How do I force a website to clear cache?

Make sure to only select the Cached data and files box to avoid clearing important information. You can also access the menu by using the shortcut: CTRL + SHIFT + DEL. Then, click the Clear button at the bottom.

What is cache memory in ASP.NET c#?

Caching is a technique of storing frequently used data/information in memory, so that, when the same data/information is needed next time, it could be directly retrieved from the memory instead of being generated by the application.


1 Answers

I think you are almost there. You need more HTML headers to support all browsers. According to this article on SO these are the ones that work on all browsers:

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

The full code for this is:

HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
HttpContext.Current.Response.AddHeader("Expires", "0");
like image 93
Patrick Hofman Avatar answered Sep 28 '22 07:09

Patrick Hofman