如何使用WCF中的SSL加密 [英] How can I use SSL encryption in WCF

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

问题描述

我从本教程简单的应用程序: WCF 4入门教程



我怎样才能实现一些加密?喜欢的东西HTTPS(SSL?)。



示例代码教程。

 静态无效的主要(字串[] args)
{

//地址配置过程的第1步:创建一个URI作为基址。
乌里baseAddress =新的URI(HTTP://本地主机:8000 / ServiceModelSamples /服务);

//托管过程的步骤2:创建的ServiceHost
的ServiceHost selfHost =新的ServiceHost(typeof运算(CalculatorService的),baseAddress);


{托管程序的


//第3步:添加服务端点。
selfHost.AddServiceEndpoint(
typeof运算(ICalculator),
新的WSHttpBinding(),
CalculatorService的);


//托管过程的步骤4:启用元数据交换。
的ServiceMetadataBehavior SMB =新的ServiceMetadataBehavior();
smb.HttpGetEnabled = TRUE;
selfHost.Description.Behaviors.Add(SMB);

//托管过程的步骤5:启动(然后停止)服务。
selfHost.Open();
Console.WriteLine(服务已经准备就绪。);
Console.WriteLine(新闻<进入>至终止服务。);
Console.WriteLine();
到Console.ReadLine();

//关闭ServiceHostBase来关闭服务。
selfHost.Close();
}
赶上(CE的CommunicationException)
{
Console.WriteLine(发生异常:{0},ce.Message);
selfHost.Abort();
}

}


解决方案

最简单的方法可能是使用的运输保障。请参见 HTTP传输安全。它介绍了如何为自托管和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加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆