2017-09-18 72 views
1

嘗試訪問我的WCF WF服務時,我一直收到相同的錯誤:「無法爲具有權限的SSL/TLS建立安全通道。」WCF WF服務,無法爲具有權限的SSL/TLS建立安全通道

我已經嘗試了一堆stackoverflow /谷歌解決方案,但沒有運氣到目前爲止。

我做了一個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> 

客戶端僅僅是運行這個簡單的控制檯:

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

具有以下的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> 

如果有人能幫助我,那將會很棒。

編輯: 因此,我似乎無法工作的事情是我的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; 
    } 
} 
+0

你嘗試使用WCF跟蹤(svctraceviewer.exe)? https://docs.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/tracing/index – Yaniv

+0

@Yaniv我會看看它,謝謝你的提示 – e11en

+0

@Yaniv沒有得到任何明智的使用跟蹤,每個錯誤都是相同的「無法爲具有權限的SSL/TLS建立安全通道」 – e11en

回答

0

那麼我能夠做到這一點很好的唯一方法就是使用HttpModule(感謝ajawad987的提示)。我仍然相信,這可以(或應該)只在Web.config中完成。

public class InitializerModule : IHttpModule 
{ 
    public void Dispose() { } 

    public void Init(HttpApplication context) 
    { 
     // Sets the TLS version for every request made to an external party 
     System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 
    } 
} 

的Web.config

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
    <add name="InitializerModule" type="WorkflowService.Helpers.InitializerModule"/> 
    </modules> 
</system.webServer> 
2

今年早些時候我遇到了類似的問題,事實證明一些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; 

以上應在應用程序啓動時調用的理想。在那之後,我想你會好到哪裏去。

旁註: .NET 4.0有針對性的應用程序將需要一個稍微不同的技巧來設置TLS版本:

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; 

而在.NET 3.5和更低,TLS 1.2甚至不支持不幸。但是,在你的情況下,它看起來像你的目標.NET 4.5所以你的好去在這方面。

編輯:我在你的客戶端代碼片斷注意到你逐位或運算的TLS版本,這是不正確。這應該是一個單一的值,SecurityProtocolType.Tls12在這種情況下。

我假設.NET Framework的基礎網絡代碼只在最後使用SecurityProtocolType.Tls,不匹配是什麼SSL/TLS證書支持。

希望有幫助。

+0

我在調用客戶端時已添加此內容(請參閱客戶端代碼)。當我直接從控制檯訪問第三方時,它可以工作,但是當我從工作流服務調用第三方時,出現此錯誤。我找不到在WCF WF服務中設置安全協議的方法。 – e11en

+0

感謝您提供關於TLS版本的提示! – e11en

+0

我應該更仔細地閱讀你的文章。我還沒找到方法,但也許你可以有一個http模塊來設置適當的TLS級別。它會在你的WCF服務之前被調用。或者實際上創建一個設置它的服務行爲。 – ajawad987

相關問題