本文介绍了如何使用WCF中的SSL加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从本教程简单的应用程序:。它介绍了如何为自托管和IIS的托管服务。



配置SSL

如果你需要的是加密的,那么这就是它。如果你也想客户端身份验证,那么客户端应该使用自己的证书,该服务必须接受。


I have simple application from this tutorial: WCF 4 Getting Started Tutorial

How can I implement some encryption? Something like HTTPS (SSL?).

Example code from tutorial.

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();
        }

    }
解决方案

The easiest way is probably to use transport security. See HTTP Transport Security. It describes how to configure SSL for both self-hosted and IIS-hosted services.

If all you need is encryption, then that's it. If you also want client authentication, then the client should use its own certificate which the service must accept.

这篇关于如何使用WCF中的SSL加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:14
查看更多