【问题标题】:Not able to run WCF https web service无法运行 WCF https Web 服务
【发布时间】:2013-02-20 20:54:38
【问题描述】:

我创建了一个WCF HTTP 自托管网络服务。现在我想把它转换成HTTPS。所以我遵循以下几点:

跟随this 页面创建certificates 并将其绑定到特定端口。 我使用mmc-> console root 创建了一个证书,并按照上面链接中的相同步骤进行操作。

然后我运行以下命令将端口与证书绑定:

netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF} 

我根据我的证书更改certhash。我还检查了Created certificate info 并得到了这个。

我还粘贴了我的项目中编写的代码以在绑定端口上运行 Web 服务:

try
  {
    m_running = true;
    private static String m_baseAddress = "https://10.0.0.1:8083";
    WebHttpBinding _binding = new WebHttpBinding();
    _binding.Security.Mode = WebHttpSecurityMode.Transport;
    _binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
    m_serviceHost = new WebServiceHost(typeof(TService), new Uri(m_serviceAddress));
 m_serviceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName,"contoso.com");
    ServiceEndpoint ep = m_serviceHost.AddServiceEndpoint(typeof(TContract), _binding, "");
     m_serviceHost.Open();
    }
     catch(Exception e){ }

每当我重建我的项目并运行它时。它总是开始一秒钟然后停止。我检查了日志,没有任何内容。

但是当我删除这一行时

m_serviceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName,"contoso.com");

并将https 替换为http。它工作正常。

【问题讨论】:

  • 您仍在使用 FindBySubjectName,主题名称为“contoso.com”。那是您证书中使用的名称吗?
  • 在您的 m_serviceHost.AddServiceEndpoint() 中,您正在添加一个未配置安全模式的新 WebHttpBinding()。您可能应该使用您之前创建的“绑定”。但令我惊讶的是,您在启动时没有看到任何错误。此外,如果您继续看到问题,请尝试添加跟踪,看看您是否获得了更多信息。
  • @MortenMertner 从哪里可以看到我的证书名称?

标签: c# visual-studio-2010 wcf ssl ssl-certificate


【解决方案1】:

这些是创建HTTPSWCF self hosted Web 服务的以下步骤。

  • 首先,使用 netsh 为端口添加命名空间: netsh http add urlacl url=https://127.0.0.1+:8085/ user=EVERYONE

  • 键入以下命令以创建客户端证书: makecert -sk RootCA -sky signature -pe -n CN=localhost -r -sr LocalMachine -ss Root MyCA.cer

  • 现在创建一个服务器证书: makecert -sk server -sky exchange -pe -n CN=localhost -ir LocalMachine -is Root -ic MyCA.cer -sr LocalMachine -ss My MyAdHocTestCert.cer

现在在\Program Files\Microsoft SDKs\Windows\v7.0A\Bin 中创建了两个新的certificate 文件,名称分别为MyCA.cer 和MyAdHocTestCert.cer

打开server certificate 即MyAdHocTestCert.cer 并从details 选项卡中选择thumbprint。

  • 选择thumbprint 并删除其中的所有空格。

  • 现在使用以下命令将端口与此证书绑定: netsh http add sslcert ipport=127.0.0.1:8085 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF} clientcertnegotiation=enable

在哪里

  • ipport : 主机地址和端口:输入与您在第一步中选择的相同的地址

  • certhash :指纹

现在您完成了证书和端口绑定。要检查所有内容,请在 cmd 中写入 netsh http show sslcert,您会得到如下信息:

现在为WSHTTPbinding编写以下代码:

WebHttpBinding _binding = new WebHttpBinding();
_binding.Security.Mode = WebHttpSecurityMode.Transport;
_binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
m_serviceHost = new WebServiceHost(typeof(Serviceclass), new Uri("https://127.0.0.1:8085/"));
             m_serviceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, "611fe7748c5883f8082351744604a8c917608290");
            ServiceEndpoint ep = m_serviceHost.AddServiceEndpoint(typeof(InstanceClass), _binding, "hello");
            m_serviceHost.Open();

现在创建你的消费者来使用这个自托管的 WS

【讨论】:

  • 这是使用 SSL 创建自托管 Web 服务的整个过程。我犯的错误是我从错误的点创建证书
猜你喜欢
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多