メッセージ セキュリティ サンプル

Download sample

このサンプルでは、basicHttpBinding とメッセージ セキュリティを使用するアプリケーションを実装する方法を示します。このサンプルは、電卓サービスを実装する「入門サンプル」に基づいています。

Noteメモ :

このサンプルのセットアップ手順とビルド手順については、このトピックの最後を参照してください。

basicHttpBinding のセキュリティ モードの値は、MessageTransportTransportWithMessageCredentialTransportCredentialOnly、および None に設定できます。サービスのサンプルの App.config ファイルでは、エンドポイント定義によって basicHttpBinding が指定され、Binding1 という名前のバインディング構成が参照されます。次のサンプル構成を参照してください。

<system.serviceModel>
  <services>
    <service name="Microsoft.ServiceModel.Samples.CalculatorService"
             behaviorConfiguration="CalculatorServiceBehavior">
     <!-- This endpoint is exposed at the base address provided by -->
     <!-- host: https://localhost:8000/ServiceModelSamples/service.-->
     <endpoint address=""
               binding="basicHttpBinding"
               bindingConfiguration="Binding1" 
               contract="Microsoft.ServiceModel.Samples.ICalculator" />
    </service>
  </services>   …
</system.serviceModel>

このバインディング構成では、security Element in basicHttpBindingmode 属性を Message に設定し、message Element in basicHttpBindingclientCredentialType 属性を Certificate に設定します。次のサンプル構成を参照してください。

<bindings>
  <basicHttpBinding>
    <!-- 
        This configuration defines the SecurityMode as Message and 
        the clientCredentialType as Certificate.
        -->
    <binding name="Binding1" >
      <security mode = "Message">
        <message clientCredentialType="Certificate"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

サービスがクライアントに対してサービス自体を認証するために使用する証明書は、serviceCredentials 要素の下にある構成ファイルの behaviors セクションで設定されます。クライアントがサービスに対してクライアント自体を認証するために使用する証明書に適用される検証モードも、clientCertificate 要素の下にある behaviors セクションで設定されます。

<!--For debugging purposes, set the includeExceptionDetailInFaults attribute to true.-->
<behaviors>
  <serviceBehaviors>
    <behavior name="CalculatorServiceBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="False" />
      <!--The serviceCredentials behavior allows one to define a -->
      <!--service certificate. A service certificate is used by a -->
      <!--client to authenticate the service and provide message -->
      <!-- protection. This configuration references the "localhost"-->
      <!--certificate installed during the setup instructions. -->
      <serviceCredentials>
        <serviceCertificate findValue="localhost"
               storeLocation="LocalMachine" 
               storeName="My" x509FindType="FindBySubjectName" />
        <clientCertificate>
          <!-- Setting the certificateValidationMode to -->
          <!-- PeerOrChainTrust means that if the certificate -->
          <!--is in the user's Trusted People store, then it is -->
          <!-- trusted without performing a validation of the -->
          <!-- certificate's issuer chain. This setting is used -->
          <!-- here for convenience so that the sample can be run -->
          <!-- without having to have certificates issued by a -->
          <!-- certificate authority (CA). -->
          <!-- This setting is less secure than the default, -->
          <!-- ChainTrust. The security implications of this -->
          <!-- setting should be carefully considered before using -->
          <!-- PeerOrChainTrust in production code. -->
          <authentication 
                       certificateValidationMode="PeerOrChainTrust" />
        </clientCertificate>
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>

同じバインディングとセキュリティの詳細が、クライアントの構成ファイルで指定されます。

次のコードを使用すると、呼び出し元の ID がサービス コンソール ウィンドウに表示されます。

Console.WriteLine("Called by {0}", ServiceSecurityContext.Current.PrimaryIdentity.Name);

このサンプルを実行する場合は、操作要求および応答はクライアントのコンソール ウィンドウに表示されます。クライアントをシャットダウンするには、クライアント ウィンドウで 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. ソリューションの C# 版または Visual Basic .NET 版をビルドするには、「Windows Communication Foundation サンプルのビルド」の手順に従います。

サンプルを同じコンピュータで実行するには

  1. Setup.bat をサンプルのインストール フォルダで実行します。これにより、サンプルの実行に必要なすべての証明書がインストールされます。

    Noteメモ :

    Setup.bat バッチ ファイルは、Windows SDK コマンド プロンプトから実行します。MSSDK 環境変数が SDK のインストール ディレクトリを指している必要があります。この環境変数は、Windows SDK コマンド プロンプトで自動設定されます。

  2. サービス アプリケーションを \service\bin で実行します。

  3. クライアント アプリケーションを \client\bin で実行します。クライアント アクティビティがクライアントのコンソール アプリケーションに表示されます。

  4. クライアントとサービス間で通信できない場合は、「トラブルシューティングのヒント」を参照してください。

  5. サンプルの使用が終わったら、Cleanup.bat を実行して証明書を削除してください。他のセキュリティ サンプルでも同じ証明書を使用します。

サンプルを別のコンピュータで実行するには

  1. サービス コンピュータにサービス バイナリ用のディレクトリを作成します。

  2. サービス プログラム ファイルを、サーバーのサービス ディレクトリにコピーします。Setup.bat、Cleanup.bat、ImportClientCert.bat の各ファイルもサーバーにコピーします。

  3. クライアント コンピュータにクライアント バイナリ用のディレクトリを作成します。

  4. クライアント プログラム ファイルを、クライアント コンピュータに作成したクライアント ディレクトリにコピーします。Setup.bat、Cleanup.bat、ImportServiceCert.bat の各ファイルもクライアントにコピーします。

  5. サーバーで setup.bat service を実行します。setup.bat service 引数を指定して実行すると、コンピュータの完全修飾ドメイン名を使用してサービス証明書が作成され、Service.cer というファイルにエクスポートされます。

  6. Service.exe.config を編集して、新しい証明書名 (<serviceCertificate> of <serviceCredentials> 要素の findValue 属性) を反映します。これは、コンピュータの完全修飾ドメイン名と同じです。さらに、ベース アドレスの値を localhost から完全修飾コンピュータ名に変更します。

  7. Service.cer ファイルを、サービス ディレクトリからクライアント コンピュータのクライアント ディレクトリにコピーします。

  8. クライアントで setup.bat client を実行します。setup.bat client 引数を指定して実行すると、client.com というクライアント証明書が作成され、Client.cer というファイルにエクスポートされます。

  9. クライアント コンピュータの Client.exe.config ファイルで、エンドポイントのアドレス値をサービスの新しいアドレスに合わせます。そのためには、localhost をサーバーの完全修飾ドメイン名に置き換えます。さらに、<defaultCertificate> ElementfindValue 属性を、サーバーの完全修飾ドメイン名になる、新しいサービス証明書の名前に変更します。

  10. Client.cer ファイルを、クライアント ディレクトリからサーバーのサービス ディレクトリにコピーします。

  11. クライアントで ImportServiceCert.bat を実行します。これにより、サービス証明書が Service.cer ファイルから CurrentUser - TrustedPeople ストアにコピーされます。

  12. サーバーで ImportClientCert.bat を実行します。これにより、クライアント証明書が Client.cer ファイルから LocalMachine - TrustedPeople ストアにインポートされます。

  13. サービス コンピュータで、コマンド プロンプトから Service.exe を起動します。

  14. クライアント コンピュータで、コマンド プロンプト ウィンドウから Client.exe を起動します。

    1. クライアントとサービス間で通信できない場合は、「トラブルシューティングのヒント」を参照してください。

サンプルの実行後にクリーンアップするには

  • サンプルの実行が終わったら、サンプル フォルダにある Cleanup.bat を実行します。

    Noteメモ :

    このサンプルを別のマシンで実行している場合、このスクリプトはサービス証明書をクライアントから削除しません。別のコンピュータで証明書を使用する Windows Communication Foundation (WCF) サンプルを実行した場合は、CurrentUser - TrustedPeople ストアにインストールされたサービス証明書を忘れずに削除してください。削除するには、コマンド certmgr -del -r CurrentUser -s TrustedPeople -c -n <Fully Qualified Server Machine Name> を実行します。たとえば、certmgr -del -r CurrentUser -s TrustedPeople -c -n server1.contoso.com となります。

Footer image

Copyright © 2007 by Microsoft Corporation.All rights reserved.