在 exe 托管的 WCF Web 服务中启用 HSTS
Posted
技术标签:
【中文标题】在 exe 托管的 WCF Web 服务中启用 HSTS【英文标题】:Enabling HSTS in an exe hosted WCF web service 【发布时间】:2021-06-04 05:58:52 【问题描述】:我有一个 WCF 服务,它使用 ServiceHost 类托管在 exe 中。我称它为“网络服务”,因为它通过 https 进行侦听,所以我可以从网络浏览器 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>
【问题讨论】:
您在 WCF 中使用了哪些绑定?您是在使用 SOAP 的 HTTP REST 功能,还是在使用 SOAP over HTTP?您可以发布您的app.config
文件以显示您的<system.serviceModel>
配置吗?您是否在在代码中进行任何配置?
基本上,您可以在 WCF 代码中访问 WebOperationContext
吗?
另外,正如您现在可能知道的那样,WCF 是......不必要的复杂,具有真正疯狂的重载术语(绑定,行为,端点,aieee!)和令人困惑的过度配置 - 我'我很高兴 WCF 死了。您是否有从 WCF 迁移到更现代、更精简的计划,例如 ASP.NET Core?
我无权访问 WebOperationContext。
我们的产品太可怕了,我看不到从 WCF 迁移的机会。 :(
【参考方案1】:
我已尝试将其添加到
<system.ServiceModel>
部分,但无效。我不确定是否可以在没有 IIS 支持的情况下处理<system.webServer>
部分。如果没有 IIS(或 Apache),我也不确定 HSTS 是否是一个有效的概念。
正确。 <system.webServer>
元素特定于 IIS 7 及更高版本(包括 IIS Express)。
app.config
和 web.config
文件的架构是明确定义的 - 这不是魔术:移动元素不会导致库中的功能突然在另一个不相关的库中工作图书馆。
(虽然<system.web>
(ASP.NET) 和 <system.webServer>
(IIS) 之间存在一些共性,但这只是一个巧合 - 或者因为托管管道系统,我明白为什么这会引起混乱0。
您的<rewrite>
部分根本不起作用,因为这取决于安装了 IIS URL 重写模块的 IIS 和(它是一个可选组件,默认情况下未安装)。
如果没有 IIS(或 Apache),我也不确定 HSTS 是否是一个有效的概念。
HSTS 适用于任何 HTTP 服务:它只是意味着网络服务器(或网络应用程序,它不必是主机/服务器问题)正在发送Strict-Transport-Security
标题。
您仍然可以在 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 操作/端点/等。这里的好处是你不必担心理解 WCF 的“行为”系统。
更好的方法:IClientMessageInspector
:
见https://weblogs.asp.net/paolopia/writing-a-wcf-message-inspector
在这里总结起来有点复杂,抱歉。
最佳方法:自定义行为
自从我上次编写任何 WCF 代码以来已经字面意思整整十年了,我已经忘记了如何进行自定义行为。对不起,我不能再有用了:/
【讨论】:
以上是关于在 exe 托管的 WCF Web 服务中启用 HSTS的主要内容,如果未能解决你的问题,请参考以下文章