【问题标题】:How to setup selfhosted https WCF service with embedded certificates on client and server?如何在客户端和服务器上使用嵌入式证书设置自托管 https WCF 服务?
【发布时间】:2010-07-30 15:03:54
【问题描述】:

我正在创建一个简单的 WCF 服务来接收崩溃报告。

该服务将作为控制台程序自托管运行,并且必须在不安装任何证书的情况下运行。

安全方面,我需要确保客户端发送的数据只发送到我们的服务器,并且数据不会被截获。从服务器的角度来看,我还想确保连接的客户端使用特定的证书(嵌入在客户端程序集中)以阻止滥用服务。

我创建了一个自签名证书,并计划将 .cer(包含证书的公共部分)嵌入到客户端程序集中,并将包含带有私钥的证书的 PFX 嵌入到服务主机程序程序集中。 (this 让我相信我可以使用一个证书)。

我的问题是,无论如何设置,我都会收到以下错误:

“向https://localhost:8080/errorservice发出HTTP请求时出错。这可能是由于在HTTPS情况下服务器证书没有正确配置HTTP.SYS。这也可能是由于不匹配导致的客户端和服务器之间的安全绑定。”

绑定之间不应存在不匹配,因为它们是使用相同的代码创建的:

public static BasicHttpBinding CreateStreamingBinding() {
  BasicHttpBinding streamBinding = new BasicHttpBinding();
  streamBinding.TransferMode = TransferMode.StreamedRequest;
  streamBinding.MaxReceivedMessageSize = long.MaxValue;
  streamBinding.Security = new BasicHttpSecurity
  {
    Transport = new HttpTransportSecurity 
    {
      ClientCredentialType = HttpClientCredentialType.None,
      ProxyCredentialType =HttpProxyCredentialType.None
    },
    Mode = BasicHttpSecurityMode.Transport,
  };
  streamBinding.MaxBufferSize = int.MaxValue;
  streamBinding.MessageEncoding = WSMessageEncoding.Mtom;
  streamBinding.SendTimeout = new TimeSpan( 1, 0, 0, 0, 0 );
  streamBinding.ReceiveTimeout = new TimeSpan( 1, 0, 0, 0, 0 );
  return streamBinding;
}

在客户端,创建服务的代码是这样设置的(证书位置仅用于测试):

protected ErrorReportingServiceClient CreateClient() {
  X509Certificate2 cert = new X509Certificate2( @"C:\certs\reporting.cer" );

  EndpointAddress endpointAddress = new EndpointAddress( new Uri( ReportingServiceUri ));

  ErrorReportingServiceClient client =  new ErrorReportingServiceClient( CreateStreamingBinding(), endpointAddress );
  client.ClientCredentials.ServiceCertificate.DefaultCertificate = cert;
  client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
  client.ClientCredentials.ClientCertificate.Certificate = cert;

  return client;
}

在服务端设置如下:

    X509Certificate2 cert = new X509Certificate2( @"C:\certs\reporting.pfx", <password>);
    BasicHttpBinding basicHttpBinding = CreateStreamingBinding();
    host.Credentials.ClientCertificate.Certificate = cert;
    host.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
    host.Credentials.ServiceCertificate.Certificate = cert;

    host.AddServiceEndpoint( contractType, basicHttpBinding, baseAddress );

任何有关如何正确设置的帮助将不胜感激。

【问题讨论】:

    标签: https wcf


    【解决方案1】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 2019-05-28
      • 2012-01-28
      • 1970-01-01
      相关资源
      最近更新 更多