次の方法で共有


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

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

このサンプルのセットアップ手順とビルド手順は、このトピックの最後にあります。

basicHttpBindingのセキュリティ モードは、MessageTransportTransportWithMessageCredentialTransportCredentialOnlyNoneの値に設定できます。 次のサンプル サービス 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: http://localhost:8000/ServiceModelSamples/service.-->
     <endpoint address=""
               binding="basicHttpBinding"
               bindingConfiguration="Binding1"
               contract="Microsoft.ServiceModel.Samples.ICalculator" />
    </service>
  </services>
  ...
</system.serviceModel>

バインド構成では、<security> のmode属性をMessageに設定し、次のサンプル構成に示すように><message のclientCredentialType属性を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 -->
          <!-- certification 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 サンプル One-Time セットアップ手順を実行していることを確認します。

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

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

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

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

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

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

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

  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> 要素の findValue 属性内) を反映させます。 また、localhost の代わりに完全修飾マシン名を指定するようにベース アドレスの値を変更します.

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

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

  9. クライアント コンピューター上の Client.exe.config ファイルで、サービスの新しいアドレスと一致するようにエンドポイントのアドレス値を変更します。 これを行うには、localhost をサーバーの完全修飾ドメイン名に置き換えます。 また、<defaultCertificate> のfindValue属性を、サーバーの完全修飾ドメイン名である新しいサービス証明書名に変更します。

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

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

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

  13. サービス マシンで、コマンド プロンプトから Service.exe を実行します。

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

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

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

  • サンプルの実行が完了したら、samples フォルダーで Cleanup.bat を実行します。

    このスクリプトでは、複数のコンピューターでこのサンプルを実行する場合、クライアント上のサービス証明書は削除されません。 コンピューター間で証明書を使用する 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