【发布时间】:2017-03-24 05:49:49
【问题描述】:
我需要从一些应该为 iframe 呈现内容的操作中删除 X-Frame-Options: SAMEORIGIN 标头。只要它默认添加到请求中,我在Startup.cs:services.AddAntiforgery(o => o.SuppressXFrameOptionsHeader = false); 中禁用它。然后我写了一个简单的中间件:
app.Use(async (context, next) =>
{
context.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
await next();
});
响应跨域请求所需的操作用结果过滤器属性修饰:
public class SuppresXFrameOptionFilter : ResultFilterAttribute
{
public override async Task OnResultExecutionAsync(ResultExecutingContext context,
ResultExecutionDelegate next)
{
context.HttpContext.Response.Headers.Remove("X-Frame-Options");
await next();
}
}
怪事来了。第一个跨域请求失败,因为尽管过滤器最终按预期工作,但 X-Frame-Options: SAMEORIGIN 仍然存在于响应中(我在中间件中的 next() 之后检查了它 - 标头重新出现)。如果我按 F5,则标题不再在响应中,并且一切正常。这只发生在 X-Frame-Options 标头中,自定义标头已正确删除。
是什么让被删除的X-Frame-Options 再次出现在响应中?
【问题讨论】:
标签: c# asp.net-core x-frame-options