このサンプルでは、Windows 認証でメッセージ レベルのセキュリティを使用するように WSHttpBinding バインドを構成する方法を示します。 このサンプルは、入門に基づいています。 このサンプルでは、サービスはインターネット インフォメーション サービス (IIS) でホストされ、クライアントはコンソール アプリケーション (.exe) です。
注
このサンプルのセットアップ手順とビルド手順は、このトピックの最後にあります。
<wsHttpBinding> の既定のセキュリティは、Windows 認証を使用したメッセージ セキュリティです。 このサンプルの構成ファイルでは、mode
security< の>属性をMessage
に、clientCredentialType
属性をWindows
に明示的に設定します。 これらの値は、このバインディングの既定値ですが、使用例を示すために次のサンプル構成に示すように、明示的に構成されています。
<bindings>
<wsHttpBinding>
<binding>
<security mode="Message">
<message clientCredentialType="Windows"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
クライアント エンドポイントの構成は、サービス エンドポイント、バインディング、コントラクトの絶対アドレスで構成されます。 クライアント バインドは、適切な securityMode
と authenticationMode
で構成されます。
<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 キーを押して、クライアントをシャットダウンします。
サンプルを設定、ビルド、実行するには
Windows Communication Foundation サンプル のOne-Time セットアップ手順を実行していることを確認します。
ソリューションの C# または Visual Basic .NET エディションをビルドするには、「Windows Communication Foundation サンプルのビルド」の手順に従います。
シングル コンピューター構成またはクロスコンピューター構成でサンプルを実行するには、「 Windows Communication Foundation サンプルの実行」の手順に従います。