Clearing Cache in Asp.net
In computer engineering, a cache is a component that transparently stores data so that future requests for that data can be served faster. If requested data is contained in the cache, this request can be served by simply reading the cache, which is comparatively faster. But there are times when we need to clear cache to enhance security.
HOW TO CLEAR CACHE ON LOGOUT?
Suppose we have two page login.aspx and home.aspx. In login.aspx we have login panel and on the click of login button valid user can access their details in home.aspx and in home.aspx we also have a logout button and after logout we have to expires the session of user and remove the cache from browser so that previous pages cannot be visited by clicking browser back button.
// index.aspx - start
protected void btnLogin_Click(object sender, EventArgs e)
Session["emailid"] = txtUSerID.Text;
// index.aspx - end
// home.aspx - start
protected void Page_Load(object sender, EventArgs e)
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
Response.Expires = 0;
Response.CacheControl = "no-Cache";
if (Session["emailid"] == null)
protected void btnLogout_Click(object sender, EventArgs e)
Session["emailid"] = null;
string nextpage = "index.aspx";
// home.aspx - end
The code above removes the cache from the browser and prevents unauthorized access to the previous page after log out and cannot able to access the previous page by clicking on the browser back button.