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

我们两清 提交于 2020-01-23 01:02:43

问题


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";
}

回答1:


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");



回答2:


response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.setHeader("Pragma", "no-cache"); 
response.setDateHeader("Expires", 0); 

These header will only work on pages on which they are included not for all the web application, so either you should add filter which include this header to all pages or you can disable back button.



来源:https://stackoverflow.com/questions/22306090/how-to-clear-browser-cache-when-user-log-off-in-asp-net-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!