在 exe 托管的 WCF Web 服务中启用 HSTS

Enabling HSTS in an exe hosted WCF web service

我有一个 WCF 服务,它使用 ServiceHost class 托管在一个 exe 文件中。我称它为“Web 服务”,因为它通过 https 进行监听,因此我可以从 Web 浏览器对它执行 ping 操作。 (很抱歉,如果我的条款不是很准确。)

我想为此网络服务启用 HSTS。 我发现我可以通过将其放入 Web.config:

来启用它
<system.webServer>
<httpProtocol>
  <customHeaders>
    <add name="Strict-Transport-Security" value="max-age=31536000"/>
  </customHeaders>
</httpProtocol>

<rewrite>
  <rules>
    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>

  <outboundRules>
    <rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
      <match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />
      <conditions>
        <add input="{HTTPS}" pattern="on" ignoreCase="true" />
      </conditions>
      <action type="Rewrite" value="max-age=31536000" />
    </rule>
  </outboundRules>
</rewrite>

但我没有 Web.config,因为该服务未托管在 IIS 中。 我试图将其添加到 system.ServiceModel 部分,但没有效果。 我不确定是否可以在没有 IIS 支持的情况下处理 system.webServer 部分。 如果没有 IIS(或 Apache),我也不确定 HSTS 是否是一个有效的概念。

我目前阅读的所有文章,只描述了 IIS 场景。

https://www.hanselman.com/blog/how-to-enable-http-strict-transport-security-hsts-in-iis7

https://docs.microsoft.com/en-us/iis/configuration/system.applicationhost/sites/site/hsts

https://www.forwardpmx.com/insights/blog/the-ultimate-guide-to-hsts-protocol/

所以我的问题是:在这种情况下如何启用 HSTS?

编辑: 我的服务模型配置(编辑以遮盖我们的产品名称)

<system.serviceModel>
<services>
  <service behaviorConfiguration="AServiceBehavior" name="something.RemoteAccess.WebAccess.A.Core.A">
    <endpoint address="" behaviorConfiguration="AEndpointBehaviour" binding="basicHttpBinding" bindingConfiguration="secureBasicHttpBinding" name="A" bindingNamespace="uri:something.RemoteAccess.WebAccess.A" contract="something.RemoteAccess.WebAccess.A.IService"/>
    <host>
      <baseAddresses>
        <add baseAddress="https://*:4510/somethingWebAccess/A"/>
      </baseAddresses>
    </host>
  </service>
</services>
<bindings>
  <basicHttpBinding>
    <binding name="secureBasicHttpBinding">
      <security mode="Transport">
        <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

<behaviors>
  <endpointBehaviors>
    <behavior name="AEndpointBehaviour">
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="AServiceBehavior">
      <serviceMetadata httpsGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

I have tried to add this into the <system.ServiceModel> section but was ineffective. I'm not sure if the <system.webServer> section could be processed without IIS support or not. I'm also not sure if HSTS is a valid concept at all without IIS (or Apache).

正确。 <system.webServer> 元素特定于 IIS 7 及更高版本(包括 IIS Express)。

app.configweb.config 文件的架构是 well-defined - 它不是 magic:移动元素不会导致一个库中的功能突然在另一个不相关的库中工作。

(尽管 <system.web>(ASP.NET)和 <system.webServer>(IIS)之间存在 一些共性,但这只是巧合 - 或者由于托管管道系统,我可以理解为什么这会导致混淆0。


您的 <rewrite> 部分根本不起作用,因为这取决于 IIS 安装了 IIS URL 重写模块(它是一个可选组件) , 默认不安装).


I'm also not sure if HSTS is a valid concept at all without IIS (or Apache).

HSTS适用于任何 HTTP服务:它只是意味着web-server(或web-application,它不一定是host/server 关注)正在发送 Strict-Transport-Security header.

您仍然可以在 WCF 中执行此操作。

简单的方法:WebOperationContext

WCF adding additional HTTP header to HTTP response for transporting SOAP message

var context = WebOperationContext.Current;
if( context is null ) throw new InvalidOperationContext( "WebOperationContext not found. Are you running in the right thread?" );

context.OutgoingResponse.Headers.Add( "Strict-Transport-Security", "max-age=31536000" );

缺点:您需要将此代码(或对实现此代码的函数的调用)添加到所有 WCF actions/endpoints/etc。这里的好处是你不需要担心理解WCF的“行为”系统。

更好的方法:IClientMessageInspector

https://weblogs.asp.net/paolopia/writing-a-wcf-message-inspector

我在这里总结有点复杂,抱歉。

最佳方法:自定义行为

自从我上次编写任何 WCF 代码以来字面上已经整整十年了,我已经忘记了如何执行自定义行为。对不起,我不能更有用了:/