簡體   English   中英

如何在WCF中使用SSL加密

[英]How can I use SSL encryption in WCF

我在本教程中有一個簡單的應用程序: 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 <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();
        }

    }

最簡單的方法可能是使用傳輸安全性。 請參閱HTTP傳輸安全性 它描述了如何為自托管服務和IIS托管服務配置SSL。

如果你需要的只是加密,那就是它。 如果您還需要客戶端身份驗證,則客戶端應使用自己的服務必須接受的證書。

如果您想在IIS7網站上安裝https,可以試試這個:

  1. 將啟用的協議值“https”更改為網站的高級設置。
  2. 添加綁定https端口號,例如:localhost:81
  3. 將SSL設置添加到您的網站。

然后使用http:// fullqnameofyourcomputer訪問您的站點:81如果要使用與安全站點的基本綁定來訪問WCF服務,請確保在客戶端配置中添加安全模式(非無)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM