![](/img/trans.png)
[英]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.