为什么我的自签名证书不起作用?

时间:2011-11-18 09:09:41

标签: c# wcf .net-4.0 net.tcp

有点奇怪。几个月前,我写了一个WCF服务,我使用自签名证书通过安全的Net.Tcp连接在我的本地计算机上进行了测试;一切正常。然后我使用真正的SSL证书部署到云服务器,一切仍然正常。

但是现在,我需要再次在我的本地盒子上调试一些代码,并且发生了一些变化,导致我的本地WCF连接不再起作用了! (不,证书还没有过期!)大约2分钟后,我得到EndpointNotFoundException

Could not connect to net.tcp://mypc:8000/. The connection attempt lasted for a 
time span of 00:02:06.5152363. TCP error code 10060: A connection attempt failed 
because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond 
192.168.241.1:8000

以下是创建客户端连接的代码:

protected override IWebService CreateChannel() {
  var baseAddress = CommunicationManager.DebugMode ? SharedConstants.WEB_SERVER_TEST_URL : SharedConstants.WEB_SERVER_LIVE_URL;
  var factory = new DuplexChannelFactory<IWebService>(new InstanceContext(SiteServer.Instance));
  factory.Endpoint.Address = new EndpointAddress("net.tcp://{0}:{1}/".Fmt(baseAddress, SharedConstants.PORT_WEB_SERVICE));
  var binding = new NetTcpBinding(SecurityMode.Message);
  binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
  binding.SendTimeout = TimeSpan.FromMinutes(10);
  factory.Endpoint.Binding = binding;
  // to get past a strange error, do this:
  // (see http://geekswithblogs.net/RandyMichak/archive/2009/03/04/programattically-setting-the-maxitemsinobjectgraph-property-in-client.aspx)
  foreach (var op in factory.Endpoint.Contract.Operations) {
    var dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>();
    dataContractBehavior.NullOr(b => b.MaxItemsInObjectGraph = 2147483647);
  }

  var cred = SiteServer.GetCredentials();
  if (cred != null) {
    var u = factory.Credentials.UserName;
    u.UserName = cred.CustomerID.ToString();
    u.Password = cred.Password;
  }
  return factory.CreateChannel();
}

这是服务器端的配置:

<service name="MyProgram.WebService.WebService" behaviorConfiguration="mex">
  <endpoint address="net.tcp://localhost:8000" binding="netTcpBinding" contract="MyProgram.IWebService" bindingConfiguration="SecureBinding" name="MyEndPoint"></endpoint>
  <endpoint address="mex" binding="mexTcpBinding" name="MEX" contract="IMetadataExchange"/>
  <host>
    <baseAddresses>
      <add baseAddress="net.tcp://localhost:8000"/>
    </baseAddresses>
  </host>
</service>
...
<netTcpBinding>
  <binding name="SecureBinding" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647" portSharingEnabled="false" 
             maxBufferSize="2147483647" >
    <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
    <security mode="Message">
      <message clientCredentialType="UserName"/>
    </security>
  </binding>
</netTcpBinding>

2 个答案:

答案 0 :(得分:2)

您是否一直在尝试接受调试模式下的所有证书,例如:

#if DEBUG

ServicePointManager.ServerCertificateValidationCallback = 
    new RemoteCertificateValidationCallback(delegate{ return true; });

#endif

答案 1 :(得分:1)

卫生署!

自上次调试会话以来,我更新了我的防病毒/防火墙软件,新的防火墙阻止了我! 刚打开港口,一切正常。 多么尴尬......: - o