[英]WCF Service over SSL Could not establish secure channel for SSL/TLS with authority 'test-service.hostname.com'
[英]WCF WF Service, Could not establish secure channel for SSL/TLS with authority
嘗試訪問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>
客戶端只是運行此命令的簡單控制台:
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;
}
}
我在今年早些時候遇到了類似的問題,事實證明某些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證書所支持的不匹配。
希望有幫助。
好吧,我能夠做到這一點的唯一方法是使用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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.