次の方法で共有


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

このシナリオでは、メッセージ セキュリティ モードで保護された Windows Communication Foundation (WCF) クライアントとサーバーを示します。 クライアントとサービスは、Windows 資格情報を使用して認証されます。

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

特徴 説明
セキュリティ モード メッセージ
相互運用性 WCF のみ
認証 (サーバー) サーバーとクライアントの相互認証
認証 (クライアント) サーバーとクライアントの相互認証
誠実 はい(共有セキュリティ コンテキストを使用)
機密性 はい(共有セキュリティ コンテキストを使用)
トランスポート 網。TCP
バインド NetTcpBinding

サービス

次のコードと構成は、個別に実行するためのものです。 次のいずれかを行ってください:

  • 構成なしでコードを使用してスタンドアロン サービスを作成します。

  • 指定された構成を使用してサービスを作成しますが、エンドポイントは定義しません。

コード

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

// Create the binding.
NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType =
    MessageCredentialType.Windows;

// Create the URI for the endpoint.
Uri netTcpUri = new Uri("net.tcp://localhost:8008/Calculator");

// Crate the service host and add an endpoint.
ServiceHost myServiceHost = new ServiceHost
    (typeof(Calculator), netTcpUri);
myServiceHost.AddServiceEndpoint(
    typeof(ICalculator), binding, "");

// Open the service.
myServiceHost.Open();
Console.WriteLine("Listening ....");
Console.ReadLine();

// Close the service.
myServiceHost.Close();
' Create the binding.
Dim binding As New NetTcpBinding()
binding.Security.Mode = SecurityMode.Message
binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows

' Create the URI for the endpoint.
Dim netTcpUri As New Uri("net.tcp://localhost:8008/Calculator")

' Crate the service host and add an endpoint.
Dim myServiceHost As New ServiceHost(GetType(ServiceModel.Calculator), netTcpUri)
myServiceHost.AddServiceEndpoint(GetType(ICalculator), binding, "")

' Open the service.
myServiceHost.Open()
Console.WriteLine("Listening ....")
Console.ReadLine()

' Close the service.
myServiceHost.Close()

コンフィギュレーション

サービスを設定するコードの代わりに、次の構成を使用できます。

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <system.serviceModel>  
    <services>  
      <service behaviorConfiguration=""  
               name="ServiceModel.Calculator">  
        <endpoint address="net.tcp://localhost:8008/Calculator"  
                  binding="netTcpBinding"  
                  bindingConfiguration="Windows"  
                  name="WindowsOverMessage"  
                  contract="ServiceModel.ICalculator" />  
      </service>  
    </services>  
    <bindings>  
      <netTcpBinding>  
        <binding name="Windows">  
          <security mode="Message">  
            <message clientCredentialType="Windows" />  
          </security>  
        </binding>  
      </netTcpBinding>  
    </bindings>  
    <client />  
  </system.serviceModel>  
</configuration>  

顧客

次のコードと構成は、個別に実行するためのものです。 次のいずれかを行ってください:

  • コード (およびクライアント コード) を使用してスタンドアロン クライアントを作成します。

  • エンドポイント アドレスを定義しないクライアントを作成します。 代わりに、構成名を引数として受け取るクライアント コンストラクターを使用します。 例えば次が挙げられます。

    CalculatorClient cc = new CalculatorClient("EndpointConfigurationName");
    
    Dim cc As New CalculatorClient("EndpointConfigurationName")
    

コード

次のコードは、クライアントを作成します。 バインディングはメッセージ モードのセキュリティに設定され、クライアント資格情報の種類は Windows に設定されます。

// Create the binding.
NetTcpBinding myBinding = new NetTcpBinding();
myBinding.Security.Mode = SecurityMode.Message;
myBinding.Security.Message.ClientCredentialType =
    MessageCredentialType.Windows;

// Create the endpoint address.
EndpointAddress ea = new
    EndpointAddress("net.tcp://machineName:8008/Calculator");

// Create the client.
CalculatorClient cc =
    new CalculatorClient(myBinding, ea);

// Begin using the client.
try
{
    cc.Open();
    Console.WriteLine(cc.Add(200, 1111));
    Console.ReadLine();

    // Close the client.
    cc.Close();
}
' Create the binding.
Dim myBinding As New NetTcpBinding()
myBinding.Security.Mode = SecurityMode.Message
myBinding.Security.Message.ClientCredentialType = MessageCredentialType.Windows

' Create the endpoint address.
Dim ea As New EndpointAddress("net.tcp://machineName:8008/Calculator")

' Create the client.
Dim cc As New CalculatorClient(myBinding, ea)

' Begin using the client.
Try
    cc.Open()

    Console.WriteLine(cc.Add(100, 11))
    Console.ReadLine()

    ' Close the client.
    cc.Close()
Catch tex As TimeoutException
    Console.WriteLine(tex.Message)
    cc.Abort()
Catch cex As CommunicationException
    Console.WriteLine(cex.Message)
    cc.Abort()
Finally
    Console.WriteLine("Closed the client")
    Console.ReadLine()
End Try

コンフィギュレーション

クライアントのプロパティを設定するには、次の構成を使用します。

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <system.serviceModel>  
    <bindings>  
      <netTcpBinding>  
        <binding name="NetTcpBinding_ICalculator" >  
         <security mode="Message">  
            <message clientCredentialType="Windows" />  
          </security>  
        </binding>  
      </netTcpBinding>  
    </bindings>  
    <client>  
      <endpoint address="net.tcp://machineName:8008/Calculator"
                binding="netTcpBinding"  
                bindingConfiguration="NetTcpBinding_ICalculator"  
                contract="ICalculator"  
                name="NetTcpBinding_ICalculator">
      </endpoint>  
    </client>  
  </system.serviceModel>  
</configuration>  

こちらも参照ください