【问题标题】:Adding X-Frame-Options header to all pages in MVC 4 application将 X-Frame-Options 标头添加到 MVC 4 应用程序中的所有页面
【发布时间】:2013-05-05 06:11:42
【问题描述】:

我正在尝试将 X-Frame-Options 标头(值设置为“DENY”)添加到我的 MVC 4 应用程序中。我环顾四周,似乎this 是为所有页面添加的最干净的方法。

但是,当我添加此代码时,它不会构建。 OnResultExecuting 出现错误

“找不到合适的方法来覆盖。”

public class XframeOptions : ActionFilterAttribute
{
    public override void OnResultExecuting(
          System.Web.Mvc.ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.AddHeader(
            "X-Frame-Options", "DENY");
    }
}

如果这是最干净的方法,我该如何解决这个错误?有没有更好的方法在 MVC 4 应用程序中处理这个问题?

【问题讨论】:

  • 这对我有用,但在 中设置属性不起作用。这不是我第一次在 system.webServer 中设置看似被忽略的设置。为什么会这样?

标签: c# asp.net-mvc asp.net-mvc-4 http-headers x-frame-options


【解决方案1】:

如果每个页面都需要自定义 HttpModule 或 ActionFilter,则不需要它。 https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options 详细介绍了一个更简单的解决方案:

要配置 IIS 以发送 X-Frame-Options 标头,请将以下内容添加到您网站的 Web.config 文件中:

<system.webServer>
  <!-- ... -->

  <httpProtocol>
    <customHeaders>
      <add name="X-Frame-Options" value="SAMEORIGIN" />
    </customHeaders>
  </httpProtocol>

  <!-- ... -->
</system.webServer>

【讨论】:

  • 这是一个更合适的答案。无需代码,只需配置
  • 这是最好的解决方案。
  • 嗨,我刚刚将代码添加到了我的 web.config 源文件中。但是运行程序后,我在 Chrome 测试工具上看不到 X-Frame-Options。我错过了什么吗?
  • 重启后是否重启了 IIS / IIS Express?理论上没有必要,因为当您更改 web.config 时 IIS 往往会回收,但我之前一直在假设。
  • 这是最简单的解决方案
【解决方案2】:

确保您继承自 correct class:

public class XframeOptions : System.Web.Mvc.ActionFilterAttribute

在 ASP.NET MVC 4 中,Web API 具有不同的命名空间,并且由于您没有明确指定命名空间,我猜编译器选择了错误的类:

System.Web.Http.Filters.ActionFilterAttribute

【讨论】:

  • 非常感谢,就是这样。
【解决方案3】:

还有另一种方法可以做到这一点。创建一个自定义 HttpModule,如下所示:

    public class XframeOptionsModule : IHttpModule
{
    public void Dispose()
    {

    }

    public void Init(HttpApplication context)
    {
        context.PreSendRequestHeaders += this.OnPreSendRequestHeaders;
    }
    private void OnPreSendRequestHeaders(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("x-frame-options", "Deny");
    }
}

然后在 web.config 中注册这个模块

    <modules >
        <add name ="XframeOptions" type="your module's full type info"/>
    </modules>

【讨论】:

    【解决方案4】:

    您收到此错误是因为您使用了错误的方法名称而不是 OnResultExecuting 使用 OnResultExecuted。 你应该这样写你的方法:

    public class XframeOptionsFilter : System.Web.Mvc.ActionFilterAttribute
    {
        public override void OnResultExecuted(System.Web.Mvc.ResultExecutedContext filterContext)
        {
            filterContext.HttpContext.Response.AddHeader("x-frame-options", "Deny");
        }
    }
    

    【讨论】:

      【解决方案5】:

      NWebsec 允许您通过 web.config、OWIN 中间件和/或 MVC 过滤器属性设置此安全标头和其他安全标头:https://github.com/NWebsec/NWebsec/wiki

      免责声明:我是项目的维护者。

      【讨论】:

      • 但是标准的 .Net 允许您通过 web.config 设置此安全标头和其他安全标头...?
      【解决方案6】:

      要向所有 MVC 应用程序添加拒绝“x-frame-options”标头,您可以执行以下操作以避免 Clickjacking 攻击。

      using System;
      using System.Web;
      
      namespace Demo.Website.Modules
      {
          public class XfoHeaderModule : IHttpModule
          {
              public void Init(HttpApplication context)
              {
                  context.PreSendRequestHeaders += ContextPreSendRequestHeaders;
              }
      
              public void Dispose()
              {
              }
      
              private void ContextPreSendRequestHeaders(object sender, EventArgs e)
              {
                  HttpContext.Current.Response.Headers.Add("X-Frame-Options", "Deny");
              }
          }
      }
      

      将以下内容添加到 web.config

        <system.webServer>
          <modules>
            <add name="XfoHeader" type="Demo.Website.Modules.XfoHeaderModule" />
          </modules>
        </system.webServer>
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 2013-11-16
      • 2018-11-01
      • 2013-04-15
      • 2015-04-15
      • 1970-01-01
      • 2019-06-24
      相关资源
      最近更新 更多