HTTPS を介したカスタム バインディングの信頼できるセッション

Download sample

このサンプルでは、信頼できるセッションを使用した SSL トランスポート セキュリティを示します。信頼できるセッションは、WS-ReliableMessaging プロトコルを実装しています。信頼できるセッションを介して WS-Security を構築することにより、セキュリティで保護された信頼できるセッションを持つことができます。ただし、SSL による HTTP トランスポート セキュリティの使用を選択できる場合もあります。

SSL では、パケット自体のセキュリティが保護されます。WS-SecureConversation を使用して、信頼できるセッションのセキュリティを保護する場合とは異なることに注意してください。

HTTPS を介して信頼できるセッションを使用するには、カスタム バインディングを作成する必要があります。このサンプルは、電卓サービスを実装する「入門サンプル」に基づいています。カスタム バインディングは、信頼できるセッションのバインディング要素と httpsTransport elementを使用して作成されます。カスタム バインディングの構成を次に示します。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service 
          name="Microsoft.ServiceModel.Samples.CalculatorService"
          behaviorConfiguration="CalculatorServiceBehavior">
        <!-- use base address provided by host -->
        <endpoint address=""
                  binding="customBinding"
                  bindingConfiguration="reliableSessionOverHttps" 
                  contract="Microsoft.ServiceModel.Samples.ICalculator" />
        <!-- the mex endpoint is exposed as https://localhost/servicemodelsamples/service.svc/mex-->
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"/>
      </service>
    </services>

    <bindings>
      <customBinding>
        <binding name="reliableSessionOverHttps">
          <reliableSession />
          <httpsTransport />
        </binding>
      </customBinding>
    </bindings>
    
    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>

</configuration>

サンプル内のプログラム コードは、「入門サンプル」サービスのプログラム コードと同じです。このサンプルをビルドして実行する前に、証明書を作成し、Web サーバー証明書ウィザードを使用してあらかじめ割り当てておく必要があります。次に示すクライアント用構成の例のように、構成ファイル設定でエンドポイントとバインディングを定義すると、カスタム バインディングの使用が有効になります。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>

    <client>
      <!-- this endpoint has an https: address -->
      <endpoint name=""
                address="https://localhost/servicemodelsamples/service.svc" 
                binding="customBinding" 
                bindingConfiguration="reliableSessionOverHttps" 
                contract="Microsoft.ServiceModel.Samples.ICalculator" />
    </client>

      <bindings>
        <customBinding>
          <binding name="reliableSessionOverHttps">
            <reliableSession />
            <httpsTransport />
          </binding>
        </customBinding>      
    </bindings>

  </system.serviceModel>

</configuration>

アドレス指定では https:// スキームを使用しています。

このサンプルで使用する証明書は Makecert.exe で作成されたテスト証明書なので、ブラウザで https://localhost/servicemodelsamples/service.svc のような https: アドレスにアクセスしようとするとセキュリティ警告が表示されます。Windows Communication Foundation (WCF) クライアントがテスト証明書に対して問題なく動作するようにするには、クライアントにコードを追加して、セキュリティ警告を非表示にする必要があります。そのためのコードとそれに必要なクラスは、本運用の証明書を使用するときには不要です。

// This code is required only for test certificates like those created by Makecert.exe.
PermissiveCertificatePolicy.Enact("CN=ServiceModelSamples-HTTPS-Server");

このサンプルを実行する場合は、操作要求および応答はクライアントのコンソール ウィンドウに表示されます。クライアントをシャットダウンするには、クライアント ウィンドウで Enter キーを押します。

Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press <ENTER> to terminate client.

サンプルを設定、ビルド、および実行するには

  1. Windows Communication Foundation サンプルの 1 回限りのセットアップの手順」が実行済みであることを確認します。

  2. インターネット インフォメーション サービス (IIS) サーバー証明書インストール手順」が実行済みであることを確認します。

  3. ソリューションの C# 版または Visual Basic .NET 版をビルドするには、「Windows Communication Foundation サンプルのビルド」の手順に従います。

  4. 単一コンピュータ構成か複数コンピュータ構成かに応じて、「Windows Communication Foundation サンプルの実行」の手順に従います。

Footer image

Copyright © 2007 by Microsoft Corporation.All rights reserved.