2013-05-10 28 views
35

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

但是,当我添加此代码它不会生成。在OnResultExecuting上出错

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

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

如果这是这样做的干净的方式我怎样才能解决这个问题?有没有更好的方式来处理MVC 4应用程序中的这个问题?

+0

这对我有用,但在中设置属性不起作用。这不是我第一次在system.webServer中看到忽略的设置。为什么会这样? – Gullbyrd 2016-02-02 18:07:16

回答

13

确保您从correct class继承:

public class XframeOptions : System.Web.Mvc.ActionFilterAttribute 

在ASP.NET MVC 4有网络API,它有不同的命名空间,因为你还没有明确指定的命名空间我想,编译器正在复苏错误的类:

System.Web.Http.Filters.ActionFilterAttribute 
+0

非常感谢,就是这样。 – Xaxum 2013-05-10 18:09:22

2

由于你使用了错误的方法名称,而不是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

还有另一种方法来做到这一点。创建一个自定义的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> 
103

有没有需要自定义HTTP模块或ActionFilter如果你需要它的每一页。 https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options细节更简单的解决方案:

配置IIS以发送X框-选项头,添加该网站的Web.config文件:

<system.webServer> 
    ... 

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

    ... 
</system.webServer> 
+0

这是一个更合适的答案。无需代码即可配置 – Alao 2015-08-13 14:11:39

+1

这是最好的解决方案。 – 2015-11-03 11:06:04

+1

嗨,我只是将代码添加到我的web.config源文件。但是运行程序后,我看不到Chrome测试工具中的X-Frame-Options。我错过了什么? – user754952 2015-12-10 01:55:19

0

要添加拒绝“X框选项”头到所有MVC应用程序,您可以执行以下操作以避免点击劫持攻击。

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> 

enter image description here

+2

最好远离PreSendRequestHeaders事件,因为它已被弃用。请参阅:http://www.asp.net/aspnet/overview/web-development-best-practices/what-not-to-do-in-aspnet,--what-to-do-inteadis#presend – klings 2015-06-17 10:36:04

+1

您的屏幕截图中的响应具有2个X-Frame-Options标题。 [这将是无效的](https://tools.ietf.org/html/rfc7034#section-2) – Liam 2016-08-08 13:25:21

0

NWebsec可以让你设置这和其他安全头通过web.config中,OWIN中间件,和/或MVC过滤器属性:https://github.com/NWebsec/NWebsec/wiki

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

+0

但标准的.Net *可以让你通过web.config *设置这个和其他安全标头。 – Liam 2016-08-08 13:27:15

相关问题