如何在WCF中使用SSL加密

我在本教程中有一个简单的应用程序: WCF 4入门教程

我该如何实现一些加密? 像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  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(); } } 

最简单的方法可能是使用传输安全性。 请参阅HTTP传输安全性 。 它描述了如何为自托管服务和IIS托管服务配置SSL。

如果你需要的只是加密,那就是它。 如果您还需要客户端身份validation,则客户端应使用自己的服务必须接受的证书。

如果您想在IIS7网站上安装https,可以试试这个:

  1. 将启用的协议值“https”更改为网站的高级设置。
  2. 添加绑定https端口号,例如:localhost:81
  3. 将SSL设置添加到您的网站。

然后使用http:// fullqnameofyourcomputer访问您的站点:81如果要使用与安全站点的基本绑定来访问WCF服务,请确保在客户端配置中添加安全模式(非无)。