asp.net-mvc - 同时使用 NWebSec.Mvc 和 NWebSec.OWIN 时,是否需要在 2 个地方配置安全性?

标签 asp.net-mvc security owin hsts nwebsec

我有一个使用 ASP.NET Identity v2 的 ASP.NET MVC 5 站点。我正在尝试使用 NWebSec使其“变硬”。

因为该站点使用 MVC 和 Owin,所以我安装了 NWebSec.MVC 和 NWebSec.OWIN NuGet 包。

阅读文档,可以通过配置文件为 NWebSec/MVC 设置许多选项,并且可以通过 Startup.cs 文件在代码中为 NWebSec.OWIN 设置一些相同的选项。

例如,要添加HSTS,我可以在web.config中做如下操作:

  <nwebsec>
    <httpHeaderSecurityModule xmlns="http://nwebsec.com/HttpHeaderSecurityModuleConfig.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="NWebsecConfig/HttpHeaderSecurityModuleConfig.xsd">
      <securityHttpHeaders>
        <strict-Transport-Security max-age="365" includeSubdomains="true" httpsOnly="false" preload="true" />
      </securityHttpHeaders>
    </httpHeaderSecurityModule>
  </nwebsec>

...和/或 startup.cs 中的以下内容:

    public void Configuration(IAppBuilder app)
    {
        this.ConfigureAuth(app);

        app.UseHsts(o => o.MaxAge(365).IncludeSubdomains().AllResponses().Preload());
    }

我的问题是:我是否必须在两个地方都设置所有选项 - 还是只在一个地方(在这种情况下哪个更好)?

我更愿意在 web.config 文件中进行所有配置,但我不确定这是否会遗漏一些需要在 Startup.cs 文件中设置的内容。

最佳答案

您可以安全地在 web.config 中设置所有配置。 OWIN 包适用于那些喜欢使用启动类而不是 web.config 的人。

如果你应该配置例如web.config 和中间件中的 HSTS,header 将首先根据 web.config 在响应中设置。中间件稍后将在管道中运行,并根据 OWIN 配置设置 header ,而不考虑 web.config 中的内容。因此,如果在两个地方都配置了 header ,则中间件获胜。

为了这个答案的完整性:如果您使用 MVC 属性来覆盖基本配置,则在计算结果配置时,将通过 web.config 选择特定 header 的 OWIN 配置。所以 OWIN 在所有情况下都胜过 web.config。

关于asp.net-mvc - 同时使用 NWebSec.Mvc 和 NWebSec.OWIN 时,是否需要在 2 个地方配置安全性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31564503/

相关文章:

Java 代码,安全

asp.net-mvc - 将 OAuth 不记名 token (JWT) 与 MVC 结合使用

asp.net-mvc - 将Ninject转换为ASP.NET MVC 6 DI

asp.net-mvc - 你如何构建你的 URL 路由?

jquery - 禁用使用安全组件和 jQuery 的 CakePHP 表单中的输入元素

asp.net-web-api - 无法使用 Owin 注销 Web Api

asp.net-web-api - HostingEnvironment.QueueBackgroundWorkItem 与 A​​SP.NET Web API 使用 OWIN

c# - BundleConfig 类 : An exception of type 'System.NullReferenceException' occurred in mscorlib. dll 中的 MVC 错误

asp.net-mvc - ASP.NET MVC 是否有强类型 View ?

security - 您能否将 AWS 安全组配置为具有子组或嵌套组?