如何抑制X-Frame-Options SAMEORIGIN响应头?

问题描述 投票:0回答:1

我正在尝试删除X-Frame-Options SAMEORIGIN标头或将其设置为ALLOWALL

[我已经在web.config中设置了它,并且在该站点的IIS的Http响应标头中对其进行了设置,但仍然在浏览器中获得了X-Frame-Options SAMEORIGIN,并且iframe内容未呈现。

<httpProtocol>
  <customHeaders>
    <remove name="X-Powered-By" />
    <add name="Cache-Control" value="public" />
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="X-Frame-Options" value="ALLOWALL" />
  </customHeaders>
</httpProtocol>

在Firefox和Chrome中是相同的。

我还有其他地方可以找它还是可以修改它?

iframe x-frame-options response-headers
1个回答
0
投票

转到Global.asax.cs中的Application_Start()并添加此行

System.Web.Helpers.AntiForgeryConfig.SuppressXFrameOptionsHeader = true;

尽管请注意,这意味着任何人都可以在iframe中使用您的应用程序。因此,值得使用以下代码添加新文件:

using System.Web.Mvc;

namespace MyApplication
{
    public class NoIframeAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Headers.Set("X-Frame-Options", "SAMEORIGIN");
        }
    }
}

将以下行添加到FilterConfig.cs中的RegisterGlobalFilters方法中:

filters.Add(new NoIframeAttribute());

现在可以安全地返回到您的应用程序中了,您可以在应用程序中的任何位置删除xframeoptions,并使用

Response.Headers.Remove("X-Frame-Options");
© www.soinside.com 2019 - 2024. All rights reserved.