2013-02-20 155 views
1

我已经创建了一个WCF HTTP自我托管的网络服务。现在我想将其转换为HTTPS。所以我遵循以下几点:无法运行WCF 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并得到了这个。

enter image description here

我也粘贴写在我的项目绑定端口上运行的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"); 

和替换httpshttp。它工作正常。

+0

您仍在使用FindBySubjectName和“contoso.com”作为主题名称。这是你的证书中使用的名字吗? – 2013-02-20 21:36:37

+0

在你的m_serviceHost.AddServiceEndpoint()中,你正在添加一个没有配置安全模式的新WebHttpBinding()。你应该使用之前创建的'绑定'。但是我仍然很惊讶你没有看到任何错误。此外,如果您继续看到问题,请尝试添加跟踪并查看您是否获得了更多信息。 – Praburaj 2013-02-20 23:45:57

+0

@MortenMertner从哪里可以看到我的证书名称? – 2013-02-21 08:04:12

回答

1

以下是创建HTTPSWCF self hosted网络服务的步骤。

  • 首先,使用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

现在个

两个新certificate文件在\Program Files\Microsoft SDKs\Windows\v7.0A\Bin创建与MyCA.cer名称和MyAdHocTestCert.cer

打开server certificateMyAdHocTestCert.cerdetails选项卡中选择thumbprint

enter image description here

  • 选择thumbprint并拆除所有的空间。

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

其中

  • ipport :主机地址和端口:把相同的地址,你在第一步选择

  • certhash : thumbprint

现在您已完成证书和端口绑定。要检查一切都写在cmd中netsh http show sslcert和你有这样的事情:

This is just an exaple

现在写下面的代码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

+0

这是解决方案吗? – Sam 2013-02-21 20:00:39

+0

这是使用SSL创建自托管Web服务的整个过程。我犯的错误是我从错误的角度创建证书 – 2013-02-21 20:10:13