When I refresh the Web Page, it doesn't load the content from cache . The content is actually available in the cache and expires every 4 hrs - asp.net

I have these cache headers set
Context.Response.Cache.SetExpires(DateTime.Now.Add(refresh));
Context.Response.Cache.SetMaxAge(refresh);
Context.Response.Cache.SetCacheability(HttpCacheability.Private);
Context.Response.Cache.SetValidUntilExpires(true);
Context.Response.ContentType = "text/html";
Do i have to set anything else for the caching to work when page is refreshed as well?

For programmatically places a page content in client(browser), proxy and servers cache you need do this:
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(15));
Response.Cache.SetMaxAge(TimeSpan.FromSeconds(15));
Response.Cache.SetValidUntilExpires(true);
Response.Cache.SetLastModified(DateTime.Now);
Response.Cache.SetOmitVaryStar(true);

Related

Stop Chrome from sending no cache no store header

I have static content (icons etc...) served via Asp.Net
Every response gets caching added to it, like this:
Response.Cache.SetExpires(Now.AddMinute(30))
Response.Cache.SetValidUntilExpires(True)
When I browse from my office everything is fine
When one of the users browses from home, the icons are not cached. Which makes browsing very slow.
I have a log that shows the incoming requests, and the requests from this user have this header
"Cache-Control":"no-cache, no-store"
I don't know if that's the issue, and if yes, how can I solve it? Or can there be something else wrong?
Also, after setting the cache expiration, it seems that the Response.Headers are not affected. I don't see the caching info in the headers.
This is the header string. Not a word about caching.
{Server=Microsoft-IIS%2f10.0&HitID=9&X-AspNetMvc-Version=5.2}
Why are my Cache settings being ignored?
Please check your IIS cache setting. The static file setting may not related with the Response.Cache.SetExpires method().
And you can also set the Cache-Control in iis. About how to set the Cache-Control you can refer to this link.
Cache-Control

Varnish not processing ESI request when fetching the page containing esi tag from cache?

I am using ESI for not caching a fragment of page. I have written the following configuration in vcl file for not caching my fragment:
sub vcl_backend_response{
set beresp.do_esi = true;
if (bereq.url ~ "/fragment") {
set beresp.uncacheable = true;
return (deliver);
}
}
The first time when varnish brings whole page from backend, it sends
separate request for '/fragment' and puts it proper place of page.
But on refreshing the page, varnish only reuests for the main page and
it finds it in cache. Then it does not look up for esi fragment in this page and does not send request for it. Instead it is stored in the main page itself while caching main page and brings it from there while doing lookup.
I want to know how to bring the fragment from backend, everytime request for main page is made, while serving rest of content of main page from cache.
Putting unset resp.http.Etag; inside vcl_deliver subroutine worked for me.

Add Expire headers to ASP.NET?

Programmatically how do i add expire header? I am not using IIS, i'd like to do this in ASP.NET itself or maybe how to do it with apache which is what i am using.
See How to: Set Expiration Values for ASP.NET Page Caching
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
If you set expirations for a page programmatically, you must set the Cache-Control header for the cached page as well. To do so, call the SetCacheability method and pass it the HttpCacheability enumeration value Public

ASP.NET removing page from cache

I have a web application with login in system, and basically keeps you logged in if a session is still set.
Basically the problem is after the user logs out(session is terminated and user redirected to the login page), you could still technically access the last page accessed if you retype the url but if you click on anything you are redirected to the login page.
This only happens in Internet Explorer and I assume this is occuring since the pages are being stored in the cache , is there a way fix this issue?
You must use this code, this avoids the caching of the page:
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.CacheControl = "no-cache"
Response.Expires = -1
this code avoids the caching of page

ASP.net: Cache problem when logout

I can't find any post regarding the cache on logout so I'm going to ask this question. If there is a similar question. Please let me know.
I have a Logout page that basically call FormAuthentications signout method then redirect the user to the login page. It work fine but the problem is user can click on the browser's back button and get the cached page. I tried to set the no-cache header on my master page but that doesn't work.
Can someone point me to a article or tutorial or post some tips on how you handle this situation?
thank
Depending on your requirements a possible solution might be to set the Cache-Control header to "no-cache" on every authenticated page. This will prevent pages from being cached downstream. It could be achieved by writing a custom HttpModule that will set the header:
// Prevent the browser from caching the ASPX page
Response.Cache.SetNoStore();
You can also set this in your page's HEAD section by adding the following line of code:
<meta http-equiv="Cache-Control" content="no-cache" />
By doing that if a user clicks the Back button once he's been signed out, he will be redirected to the login form instead of seeing a cached version of his last page which could be a problem if he is using a public computer.
If you're using forms authentication, make sure the forms authentication cookie is deleted when the user logs out. As soon as the user goes to do anything on the cached page (the page they pressed the back button to get to), the site will ask the user to re-login, and then redirect them back to the original page, with fresh data. Viola!
Additionally, regarding caching of pages, you need to set a pretty good number of headers to turn the caching mechanism in the browser and proxy servers off:
"Expires" - set to some date in the past
"Last-Modified" - set to the current date/time
"Cache-Control" - set to "no-cache, must-revalidate"
"Pragma" - set to "no-cache"
That should just about make the page uncacheable. The date/times need to be in RFC1123 format (format specifier "R" in .net e.g. "Mon, 17 Apr 2006 21:22:48 GMT"). You would implement this as:
Response.AddHeader("Expires", new DateTime(1940, 1, 1).ToString("R"));
Response.AddHeader("Last-Modified", DateTime.Now.ToString("R"));
Response.AddHeader("Cache-Control", "no-cache, must-revalidate");
Response.AddHeader("Pragma", "no-cache");
Or something similar, depending on where you want to add all of the headers. I have had good success with this across many browsers and proxy servers, but nothing is fool-proof where page caching is concerned.
There's no foolproof way to accomplish this. The user ultimately has control over the cache settings and no-cache headers can't override these.
Is there a particular concern you are trying to address here (security?), or are you just trying to ensure that users aren't seeing stale data?
This is a hard problem to tackle. You can create a base page and in the constructor you can check that if the person is logged in or not. If the person is not logged in that simply redirect to the login page. This base page will be inherited by all the other ASP.NET pages.
IE6 seems to ignore some cache headers.
Another technique ontop of cache headers would be some clientside javascript to check for the authentication cookie and use history.Forward() if its not there.
Add this to global.asax and it will set the no-cache headers for all pages in the web application. Be sure that disabling caching is really what you want to do however - because caching is a performance benefit.
You can of course, also apply the same Response.Cache commands to pages individually.
This works in FireFox 3, IE7, and somewhat in Opera 9.6. (In Opera, it will work if you don't to any post requests. If you do, the page will still be accessible from the back button the first time, but not afterwards.)
protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
if (!Request.Path.Contains("/Content/")) //We WANT images, css, javascripts to be cached!
{
//Otherwise, all of our pages contain sensitive information, and we don't want them cached.
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); //for Opera. May only work on https sites
Response.Cache.SetNoStore();
}
}
There is no way you can actually stop is behavior people have removed the back button disabled it but the user would still have controls from short cut for any browser , my suggestion would be that for all those pages where you want the users to be logged in Put them on a POST method i.e. instead of a regular GET use post. What this would do is anytime a user presses the back button for that url the browser would ask the user with an alert box to post the last data cached for this request and there would be a server call where form authentication would work.
It is a bit crude using post where get might work but it solves this issue of yours.

Resources