Friday, June 10, 2011

Page Without Cache in C#

This question have been trouble me for whole day, just because i put my coding in wrong page. So, please make sure your page which don't want store any cache have following coding.

protected void DisableBufferingOnPage()
{
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
// set expiry date in the past
Response.Expires = -1500;
Response.CacheControl = "no-cache";
Response.Cache.SetNoStore();
Response.AppendHeader("Pragma", "no-cache");
Response.AppendHeader("Cache-Control", "no-cache");
Response.CacheControl = "no-cache";
Response.Expires = -1;
Response.ExpiresAbsolute = new DateTime(1900, 1, 1);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now);
Response.Cache.SetAllowResponseInBrowserHistory(false);
}

Please call this function at Page_Load. Also, if you record your session when log in please make sure it is clear and abandon when log out.

Session.Clear();
Session.Abandon();

So, when you log out even you click back button also cannot go previous page unless you log in.

Sharing is caring~ Cheers~ =)


No comments:

Post a Comment