我们在AccountController.Login
行动中使用了[HttpGet]
[ResponseCache(NoStore = true)]
[AllowAnonymous]
public IActionResult Login(string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
return View();
}
。
NoStore
根据文件:
true
会覆盖大多数其他属性。当此属性设置为Cache-Control
时,"no-store"
标头将设置为~/Account/Login
。
在新的私有Firefox窗口中,当我们导航到Cache-Control:"no-cache"
Content-Type:"text/html; charset=utf-8"
Date:"Mon, 26 Dec 2016 21:50:27 GMT"
Pragma:"no-cache"
Server:"Kestrel"
Transfer-Encoding:"chunked"
时,我们会收到以下响应标头。
Cache-Control
我们正在使用ASP.NET Core 1.1.0。我们如何将no-store
标题设置为php -S localhost:8000 -t public/
?
答案 0 :(得分:4)
问题在于ASP.NET Core防伪nukes all user set headers and adds its own。为了解决这个问题,我们添加了自己的IHtmlGenerator
。
// Startup.cs
services.AddSingleton<
Microsoft.AspNetCore.Mvc.ViewFeatures.IHtmlGenerator,
OurHacks.HtmlGeneratorHack>();
// HtmlGeneratorHack.cs (subsclasses DefaultHtmlGenerator)
public override IHtmlContent GenerateAntiforgery(ViewContext viewContext)
{
var result = base.GenerateAntiforgery(viewContext);
viewContext
.HttpContext
.Response
.Headers[HeaderNames.CacheControl]
= "no-cache, max-age=0, must-revalidate, no-store";
return result;
}
答案 1 :(得分:0)
任何实现表单标记帮助程序的页面,即asp-action,asp-controller,都将清除缓存控制设置。我只是添加了这个,因为上面的答案可能并不清楚。
这令人非常沮丧,我希望这些答案有助于某人。