WCF WF服务,无法通过授权为SSL / TLS建立安全通道

e11*_*1en 1 c# ssl wcf workflow-foundation

尝试访问WCF WF服务时,我始终收到相同的错误:“无法使用授权建立SSL / TLS的安全通道。”

我已经尝试了很多stackoverflow / google解决方案,但到目前为止还没有运气。

我已经制作了WCF WF服务,该服务连接到使用证书进行验证的第三方。我的Web.config看起来像这样:

<?xml version="1.0"?>
<configuration>
<appSettings>
  <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
  <compilation debug="true" strict="false" explicit="true" 
                            targetFramework="4.5.1"/>
  <httpRuntime targetFramework="4.5.1"/>
</system.web>
<system.serviceModel>
  <bindings>      
    <basicHttpsBinding>
      <binding name="binding1">
        <security mode="TransportWithMessageCredential">
          <message clientCredentialType="Certificate" />
        </security>
      </binding>
    </basicHttpsBinding>
  </bindings>
  <client>
    <!-- Test Endpoint -->
    <endpoint address="https://example.com" 
              behaviorConfiguration="endpointBehavior" 
              binding="basicHttpsBinding" 
              bindingConfiguration="binding1" 
              contract="ContractPortType" 
              name="Port">
    </endpoint>
  </client>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>        
      </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="endpointBehavior">
        <clientCredentials>          
          <clientCertificate findValue="SOMETHUMBPRINT" 
                              storeLocation="LocalMachine" 
                              storeName="My" 
                              x509FindType="FindByThumbprint" />
        </clientCredentials>
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https"/>
  </protocolMapping>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
                             multipleSiteBindingsEnabled="true" 
                             minFreeMemoryPercentageToActivateService="1"/>
</system.serviceModel>
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

客户端只是运行此命令的简单控制台:

using (var client = new ServiceClient())
{
     System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
     var msg = client.GetData();
     Console.WriteLine(msg);
 }
Run Code Online (Sandbox Code Playgroud)

具有以下App.config:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <system.serviceModel>
    <bindings>     
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService"/>
      </basicHttpBinding>
    </bindings>

    <client>
      <endpoint address="http://localhost:55670/GetData.xamlx"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
        contract="Reference.IService" name="BasicHttpBinding_IService" behaviorConfiguration="endpointBehavior" />
    </client>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="endpointBehavior">
          <clientCredentials>
            <clientCertificate findValue="SOMETHUMBPRINT"
                               storeLocation="LocalMachine"
                               storeName="My"
                               x509FindType="FindByThumbprint" />
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

如果有人可以帮助我,那就太好了。

编辑:所以我似乎无法正常工作的是WCF WF服务和第三方服务之间所需的SSL连接。当我直接从客户端调用第三方服务时,可以设置System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 但是我找不到对WCF WF服务执行相同操作的方法。

编辑:因此,我可以通过创建活动然后在调用第三方活动之前调用该活动来使其工作,然后它起作用,但是必须有更好的方法!

public sealed class SetTlsVersion : CodeActivity
{
    protected override void Execute(CodeActivityContext context)
    {
        System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    }
}
Run Code Online (Sandbox Code Playgroud)

aja*_*987 5

我在今年早些时候遇到了类似的问题,事实证明某些SSL证书使用TLS 1.2。

.NET 4.5将TLS版本默认为v1.1。可以通过以下代码段在您的代码中对此进行更改:

using System.Net;
// ...
// In your application startup flow, set the security protocol to TLS 1.2.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Run Code Online (Sandbox Code Playgroud)

理想情况下,应在应用程序启动时调用以上内容。在那之后,我认为您会很好。

旁注:面向.NET 4.0的应用程序将需要稍微不同的技巧来设置TLS版本:

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
Run Code Online (Sandbox Code Playgroud)

在.NET 3.5及更低版本中,不幸的是,甚至不支持TLS 1.2。但是对于您而言,它看起来像是针对.NET 4.5的目标,因此在此方面挺好的。

编辑:我在您的客户端代码片段中注意到您按位或TLS版本,这是不正确的。SecurityProtocolType.Tls12在这种情况下,它应该是单个值。

我假设底层的.NET Framework网络代码仅SecurityProtocolType.Tls在最后使用,它与SSL / TLS证书支持的内容不匹配。

希望有帮助。