次の方法で共有


メッセージ セキュリティ ウィンドウ

このサンプルでは、Windows 認証でメッセージ レベルのセキュリティを使用するように WSHttpBinding バインドを構成する方法を示します。 このサンプルは、入門に基づいています。 このサンプルでは、サービスはインターネット インフォメーション サービス (IIS) でホストされ、クライアントはコンソール アプリケーション (.exe) です。

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

<wsHttpBinding> の既定のセキュリティは、Windows 認証を使用したメッセージ セキュリティです。 このサンプルの構成ファイルでは、modesecurity< の>属性をMessageに、clientCredentialType属性をWindowsに明示的に設定します。 これらの値は、このバインディングの既定値ですが、使用例を示すために次のサンプル構成に示すように、明示的に構成されています。

<bindings>
    <wsHttpBinding>
        <binding>
            <security mode="Message">
                <message clientCredentialType="Windows"/>
            </security>
        </binding>
    </wsHttpBinding>
</bindings>

クライアント エンドポイントの構成は、サービス エンドポイント、バインディング、コントラクトの絶対アドレスで構成されます。 クライアント バインドは、適切な securityModeauthenticationModeで構成されます。

<system.serviceModel>
  <client>
    <endpoint address=
            "http://localhost/servicemodelsamples/service.svc"
            binding="wsHttpBinding"
            bindingConfiguration="Binding1"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  </client>

  <bindings>
    <wsHttpBinding>
      <!-- The default security for the WSHttpBinding is -->
      <!-- Message security using Windows authentication. -->
      <!-- This configuration explicitly defines the security mode -->
      <!-- as Message and the clientCredentialType as Windows -->
      <!-- for demonstration purposes. -->
      <binding name="Binding1">
        <security mode="Message">
          <message clientCredentialType="Windows"/>
        </security>
      </binding>
    </wsHttpBinding>
  </bindings>
</system.serviceModel>

ServiceSecurityContextを使用して呼び出し元の ID にアクセスする方法を示すために、サービスのソース コードが変更されました。

public string GetCallerIdentity()
{
    // The Windows identity of the caller can be accessed on the ServiceSecurityContext.WindowsIdentity.
    return OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name;
}

サンプルを実行すると、操作要求と応答がクライアント コンソール ウィンドウに表示されます。 呼び出された最初のメソッド ( GetCallerIdentity ) は、呼び出し元 ID の名前をクライアントに返します。 コンソール ウィンドウで Enter キーを押して、クライアントをシャットダウンします。

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

  1. Windows Communication Foundation サンプル One-Time セットアップ手順を実行していることを確認します。

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

  3. シングル コンピューター構成またはクロスコンピューター構成でサンプルを実行するには、「 Windows Communication Foundation サンプルの実行」の手順に従います。