【问题标题】:How can I use SSL encryption in WCF如何在 WCF 中使用 SSL 加密
【发布时间】:2011-05-17 08:14:27
【问题描述】:

我有本教程中的简单应用程序:WCF 4 Getting Started Tutorial

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

    }

【问题讨论】:

  • 您如何托管 WCF 服务 - 使用 WCF 内置 TCP 或 HTTP 绑定,或通过 IIS 之类的东西?
  • 本教程中的一切。
  • 您应该首先了解您的服务使用什么配置,并能够在您的问题中描述它。我们为什么要阅读整个教程?

标签: c# wcf visual-studio-2010 ssl


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    如果你想在你的 IIS7 网站中有 https,你可以试试这个:

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

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

    【讨论】:

    • 是否有充分的理由不使用默认的 HTTPS 端口 443?我认为这里的复杂步骤是为 IIS 生成一个 SSL 证书,但是,如果你还没有一个 - 我不知道是否有用于 IIS 7 的 SelfSSL 工具版本(它可以为你完成所有工作) IIS 6)。
    • @Rup: 实际上有一个 IIS7 的自我证书。我只是在我的开发工作中使用它,这就是我使用 81 端口的原因,我有多个站点对应于我在 TFS 中的分支。
    • 很好,我没见过。这是ScottGu's blog about it
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多