次の方法で共有


メッセージ セキュリティと証明書クライアント

次のシナリオでは、メッセージ セキュリティ モードを使用して保護されている Windows Communication Foundation (WCF) クライアントおよびサービスを示します。クライアントとサービスは、どちらも証明書を使用して認証されます。詳細な情報については、次のページを参照してください。 「分散アプリケーションのセキュリティ」を参照してください。

サンプル アプリケーションについては、「Message Security Certificate」を参照してください。

証明書を使用したメッセージ セキュリティ

特性 説明

セキュリティ モード

メッセージ

相互運用性

WCF のみ

認証 (サーバー)

初めてトランスポート層セキュリティ (TLS: Transport Layer Security) ネゴシエーションを使用する場合は、サービス証明書を使用してサーバーを認証する必要があります。

認証 (クライアント)

クライアント証明書と TLS ネゴシエーションを使用します。

整合性

はい、TLS プロトコルを使用します

機密性

はい、TLS プロトコルを使用します

トランスポート

HTTP

バインディング

WSHttpBinding

サービス

次のコードと構成は、別々に実行することを想定しています。次のいずれかを行います。

  • 構成を使用せずに、コードを使用してスタンドアロン サービスを作成する。
  • 提供された構成を使用してサービスを作成するが、エンドポイントを定義しない。

コード

次のコードは、メッセージ セキュリティを使用するサービス エンドポイントを作成し、セキュリティで保護されたコンテキストを確立する方法を示しています。

構成

コードの代わりに次の構成を使用できます。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceCredentialsBehavior">
          <serviceCredentials>
            <serviceCertificate findValue="Contoso.com"
                                x509FindType="FindBySubjectName" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="ServiceCredentialsBehavior" 
               name="ServiceModel.Calculator">
        <endpoint address="https://localhost/Calculator" 
                  binding="wsHttpBinding"
                  bindingConfiguration="MessageAndCerficiateClient" 
                  name="SecuredByClientCertificate"
                  contract="ServiceModel.ICalculator" />
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ICalculator">
          <security mode="Message">
            <message clientCredentialType="Certificate" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client />
  </system.serviceModel>
</configuration>

クライアント

次のコードと構成は、別々に実行します。以下のいずれかの操作を行います。

  • コード (およびクライアント コード) を使用してスタンドアロン クライアントを作成する。
  • エンドポイント アドレスを定義しないクライアントを作成する。代わりに、引数として構成名を受け取るクライアント コンストラクタを使用する。次に例を示します。

コード

クライアントを作成する場合のコード例を次に示します。バインディングではメッセージ モード セキュリティを使用し、クライアント資格情報の種類は Certificate に設定します。

構成

次の構成は、エンドポイントの動作を使用してクライアント証明書を指定します。証明書の詳細については、「証明書の使用」を参照してください。また、コード内で <identity> 要素を使用して、予想されるサーバー ID のドメイン ネーム システム (DNS: Domain Name System) を指定します。ID 詳細については、 、「サービス ID と認証」を参照してください。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="endpointCredentialsBehavior">
          <clientCredentials>
            <clientCertificate findValue="Cohowinery.com" 
               storeLocation="LocalMachine"
              x509FindType="FindBySubjectName" />
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ICalculator" >
          <security mode="Message">
            <message clientCredentialType="Certificate" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://machineName/Calculator" 
                behaviorConfiguration="endpointCredentialsBehavior"
                binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_ICalculator"
                contract="ICalculator"
                name="WSHttpBinding_ICalculator">
        <identity>
          <dns value="Contoso.com" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

関連項目

概念

セキュリティの概要
サービス ID と認証
証明書の使用