2011-05-17 77 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