2013-02-20 154 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