2011-05-17 35 views
4

SSL加密,我從這個教程簡單的應用:WCF 4 Getting Started Tutorial如何使用WCF中

我怎麼能實現一些加密?像HTTPS(SSL?)。

來自教程的示例代碼。

static void Main(string[] args) 
    { 

     // Step 1 of the address configuration procedure: Create a URI to serve as the base address. 
     Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service"); 

     // Step 2 of the hosting procedure: Create ServiceHost 
     ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress); 

     try 
     { 


      // Step 3 of the hosting procedure: Add a service endpoint. 
      selfHost.AddServiceEndpoint(
       typeof(ICalculator), 
       new WSHttpBinding(), 
       "CalculatorService"); 


      // Step 4 of the hosting procedure: Enable metadata exchange. 
      ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); 
      smb.HttpGetEnabled = true; 
      selfHost.Description.Behaviors.Add(smb); 

      // Step 5 of the hosting procedure: Start (and then stop) the service. 
      selfHost.Open(); 
      Console.WriteLine("The service is ready."); 
      Console.WriteLine("Press <ENTER> to terminate service."); 
      Console.WriteLine(); 
      Console.ReadLine(); 

      // Close the ServiceHostBase to shutdown the service. 
      selfHost.Close(); 
     } 
     catch (CommunicationException ce) 
     { 
      Console.WriteLine("An exception occurred: {0}", ce.Message); 
      selfHost.Abort(); 
     } 

    } 
+0

你是如何託管的WCF服務 - 使用WCF內置的TCP或HTTP綁定,或通過類似IIS? – Rup 2011-05-17 08:50:52

+0

這就像本教程。 – Ichibann 2011-05-17 08:55:48

+1

您應該首先了解您的服務使用哪種配置,並且能夠在您的問題中對其進行描述。爲什麼我們應該閱讀整個教程? – 2011-05-17 09:24:31

回答

4

最簡單的方法可能是使用傳輸安全。見HTTP Transport Security。它描述瞭如何爲自託管服務和IIS託管服務配置SSL。

如果你只需要加密,就是這樣。如果您還想要客戶端身份驗證,則客戶端應使用自己的服務必須接受的證書。

1

如果你想有HTTPS你IIS7的網站,你可以試試這個:

  1. 更改啓用協議價值「https」開頭的網站情況的預先設置。
  2. 添加綁定https端口號例如:localhost:81
  3. 將SSL設置添加到您的網站。

然後使用http://fullyqnameofyourcomputer:81 如果你想與基本的結合訪問WCF服務與安全站點訪問你的網站,只要確保你在你的客戶端的配置添加securicty模式(不無)。

+0

是否有充分理由不使用默認的HTTPS端口443?我認爲這裏的複雜步驟是爲IIS生成一個SSL證書,但是,如果您還沒有一個 - 我不知道是否有適用於IIS 7的SelfSSL工具版本(它爲您完成所有工作IIS 6)。 – Rup 2011-05-17 09:45:01

+0

@Rup:IIS7實際上有一個自我證書。我只是使用它在我的開發工作多數民衆贊成爲什麼即時通訊使用81端口,我有多個網站對應我的分支機構在TFS。 – 2011-05-17 09:50:34

+1

整潔,我沒有看到。這裏是[ScottGu的博客](http://weblogs.asp.net/scottgu/archive/2007/04/06/tip-trick-enabling-ssl-on-iis7-using-self-signed-certificates.aspx) – Rup 2011-05-17 09:54:04