次の方法で共有


ユーザー名クライアントを使用したメッセージ セキュリティ

メッセージ レベルのセキュリティで保護された Windows Communication Foundation (WCF) サービスとクライアントを次の図に示します。サービスは X.509 証明書を使用して認証されます。クライアントはユーザー名とパスワードを使用して認証されます。

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

ユーザー名認証を使用したメッセージ セキュリティ

特性 説明

セキュリティ モード

メッセージ

相互運用性

Windows Communication Foundation (WCF) のみ

認証 (サーバー)

初期ネゴシエーションにはサーバー認証が必要

認証 (クライアント)

ユーザー名/パスワード

整合性

はい、共有のセキュリティ コンテキストを使用します

機密性

はい、共有のセキュリティ コンテキストを使用します

トランスポート

HTTP

バインディング

WSHttpBinding

サービス

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

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

コード

次のコードは、メッセージ セキュリティを使用するサービス エンドポイントの作成方法を示します。

構成

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

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

クライアント

コード

クライアントを作成する場合のコード例を次に示します。バインディングはメッセージ モード セキュリティに対して行い、クライアント資格情報の種類は UserName に設定します。ユーザー名とパスワードの指定はコードを使用する場合に限られます (構成可能ではありません)。ユーザー名とパスワードを返すコードは、アプリケーション レベルで実行される必要があるため、ここには示しません。たとえば、Windows フォーム ダイアログ ボックスを使用してユーザーにデータを照会します。

構成

クライアントを構成する場合のコード例を次に示します。バインディングはメッセージ モード セキュリティに対して行い、クライアント資格情報の種類は UserName に設定します。ユーザー名とパスワードの指定はコードを使用する場合に限られます (構成可能ではありません)。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ICalculator" >
          <security mode="Message">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://machineName/Calculator" 
                binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_ICalculator" 
                contract="ICalculator"
                name="WSHttpBinding_ICalculator">
        <identity>
          <dns value ="Contoso.com" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

関連項目

概念

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

その他の技術情報

Message Security User Name
<identity>