セキュリティで保護されていないインターネット環境のクライアントとサービス

セキュリティで保護されていないパブリックな Windows Communication Foundation (WCF) クライアントとサービスの例を次の図に示します。

セキュリティで保護されていないインターネット クライアントとサービスのシナリオ

特性 説明

セキュリティ モード

なし

トランスポート

HTTP

バインディング

BasicHttpBinding (コードを使用する場合) または <basicHttpBinding> 要素 (構成を使用する場合)。

相互運用性

既存の Web サービス クライアントとサービスを使用する

認証

なし

整合性

なし

機密性

なし

サービス

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

  • 構成を使用せずに、コードを使用してスタンドアロン サービスを作成します。

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

コード

次のコードは、セキュリティで保護されないエンドポイントを作成する方法を示しています。既定では、BasicHttpBinding のセキュリティ モードは None に設定されます。

Dim httpUri As New Uri("https://localhost/Calculator")

' Create the ServiceHost.
Dim myServiceHost As New ServiceHost(GetType(Calculator), httpUri)

' Create a binding that uses HTTP. By default, 
' this binding has no security.
Dim b As New BasicHttpBinding()

' Add an endpoint to the service.
myServiceHost.AddServiceEndpoint(GetType(ICalculator), b, "")
' Open the service and wait for calls.
AddMexEndpoint(myServiceHost)
myServiceHost.Open()
Console.Write("Listening....")
Console.ReadLine()
' Close the service when a key is pressed.
myServiceHost.Close()
Uri httpUri = new Uri("https://localhost/Calculator");

// Create the ServiceHost.
ServiceHost myServiceHost = new ServiceHost(typeof(Calculator), httpUri);

// Create a binding that uses HTTP. By default, 
// this binding has no security.
BasicHttpBinding b = new BasicHttpBinding();

// Add an endpoint to the service.
myServiceHost.AddServiceEndpoint(typeof(ICalculator), b, "");
// Open the service and wait for calls.
AddMexEndpoint(ref myServiceHost);
myServiceHost.Open();
Console.Write("Listening....");
Console.ReadLine();
// Close the service when a key is pressed.
myServiceHost.Close();

サービス構成

次のコードは、構成を使用して同一のエンドポイントをセットアップします。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <behaviors />
    <services>
      <service behaviorConfiguration="" name="ServiceModel.Calculator">
        <endpoint address="https://localhost/Calculator" 
                  binding="basicHttpBinding"
                  bindingConfiguration="Basic_Unsecured" 
                  name="BasicHttp_ICalculator"
                  contract="ServiceModel.ICalculator" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="Basic_Unsecured" />
      </basicHttpBinding>
    </bindings>
    <client />
  </system.serviceModel>
</configuration>

クライアント

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

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

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

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

コード

次のコードは、セキュリティで保護されていないエンドポイントにアクセスする基本的な WCF クライアントを示します。

' Create an instance of the BasicHttpBinding. 
' By default, there is no security.
Dim myBinding As New BasicHttpBinding()

' Create the address string, or get it from configuration.
Dim httpUri As String = "https://localhost/Calculator"

' Create an endpoint address with the address.
Dim myEndpoint As New EndpointAddress(httpUri)

' Create an instance of the WCF client. The client
' code was generated using the Svcutil.exe tool.
Dim cc As New CalculatorClient(myBinding, myEndpoint)
Try
    cc.Open()
    ' Begin using the calculator.
    Console.WriteLine(cc.Divide(100, 2))
    
    ' 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
// Create an instance of the BasicHttpBinding. 
// By default, there is no security.
BasicHttpBinding myBinding = new BasicHttpBinding();

// Create the address string, or get it from configuration.
string httpUri = "https://localhost/Calculator";

// Create an endpoint address with the address.
EndpointAddress myEndpoint = new EndpointAddress(httpUri);

// Create an instance of the WCF client. The client
// code was generated using the Svcutil.exe tool.
CalculatorClient cc = new CalculatorClient(myBinding, myEndpoint);
try
{
    cc.Open();
    // Begin using the calculator.
    Console.WriteLine(cc.Divide(100, 2));

    // Close the client.
    cc.Close();
}
catch (TimeoutException tex)
{
    Console.WriteLine(tex.Message);
    cc.Abort();
}
catch (CommunicationException cex)
{
    Console.WriteLine(cex.Message);
    cc.Abort();
}
finally
{
    Console.WriteLine("Closed the client");
    Console.ReadLine();
}

クライアントの設定

クライアントを構成する場合のコード例を次に示します。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ICalculator" >
          <security mode="None">
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://localhost/Calculator/Unsecured"
          binding="basicHttpBinding" 
          bindingConfiguration="BasicHttpBinding_ICalculator"
          contract="ICalculator" 
          name="BasicHttpBinding_ICalculator" />
    </client>
  </system.serviceModel>
</configuration>

参照

概念

セキュリティの概要

その他のリソース

一般的なセキュリティ シナリオ
Windows Server AppFabric のセキュリティ モデル