基本認証でのトランスポート セキュリティ

次の図は、Windows Communication Foundation (WCF) のサービスとクライアントを示しています。サーバーには、SSL (Secure Sockets Layer) に使用できる有効な X509 証明書が必要であり、クライアントはサーバーの証明書を信頼する必要があります。さらに、Web サービスには使用可能な SSL が既に実装されています。インターネット インフォメーション サービス (IIS) で基本認証を有効にする方法詳細情報、https://go.microsoft.com/fwlink/?LinkId=83822 を参照してください。

基本認証でのトランスポート セキュリティ

特性 説明

セキュリティ モード

トランスポート

相互運用性

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

認証 (サーバー)

認証 (クライアント)

○ (HTTPS を使用)

○ (ユーザー名とパスワードを使用)

整合性

機密性

トランスポート

HTTPS

バインディング

WSHttpBinding

サービス

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

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

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

コード

次のコードでは、転送セキュリティ用の Windows ドメイン ユーザー名とパスワードを使用するサービス エンドポイントを作成する方法を示します。サービスには、クライアントに対する認証を行うための X 509 証明書が必要になります。詳細については、「証明書の使用」および「方法 : SSL 証明書を使用してポートを構成する」を参照してください。

' Create the binding.
Dim binding As New WSHttpBinding()
binding.Security.Mode = SecurityMode.Transport
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic

' Create the URI for the endpoint.
Dim httpUri As New Uri("https://localhost/Calculator")

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

' Open the service.
myServiceHost.Open()
Console.WriteLine("Listening...")
Console.WriteLine("Press Enter to exit.")
Console.ReadLine()

' Close the service. 
myServiceHost.Close()
// Create the binding.
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType =
    HttpClientCredentialType.Basic;

// Create the URI for the endpoint.
Uri httpUri = new Uri("https://localhost/Calculator");

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

// Open the service.
myServiceHost.Open();
Console.WriteLine("Listening...");
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();

// Close the service. 
myServiceHost.Close();

構成

次の例では、トランスポート レベルのセキュリティの基本認証を使用するサービスを構成します。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="UsernameWithTransport">
                    <security mode="Transport">
                        <transport clientCredentialType="Basic" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <services>
            <service name="BasicAuthentication.Calculator">
                <endpoint address="https://localhost/Calculator"
                          binding="wsHttpBinding" 
                          bindingConfiguration="UsernameWithTransport"
                          name="BasicEndpoint" 
                          contract="BasicAuthentication.ICalculator" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

クライアント

コード

次のコードは、ユーザー名とパスワードが含まれるクライアント コードを示しています。ユーザーは、有効な Windows ユーザー名とパスワードを指定する必要があります。ユーザー名とパスワードを返すコードは、ここに示されていません。ダイアログボックスまたは他のインターフェースを使用して、ユーザーにこれらの情報を照会してください。

ms733775.note(ja-jp,VS.100).gif注 :
ユーザー名とパスワードは、コードを使ってのみ設定できます。

' Create the binding.
Dim myBinding As New WSHttpBinding()
myBinding.Security.Mode = SecurityMode.Transport
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic

' Create the endpoint address. Note that the machine name 
' must match the subject or DNS field of the X.509 certificate
' used to authenticate the service. 
Dim ea As New EndpointAddress("https://machineName/Calculator")

' Create the client. The code for the calculator 
' client is not shown here. See the sample applications
' for examples of the calculator code.
Dim cc As New CalculatorClient(myBinding, ea)

' The client must provide a user name and password. The code
' to return the user name and password is not shown here. Use
' a database to store the user name and passwords, or use the 
' ASP.NET Membership provider database.
cc.ClientCredentials.UserName.UserName = ReturnUsername()
cc.ClientCredentials.UserName.Password = ReturnPassword()

' 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
// Create the binding.
WSHttpBinding myBinding = new WSHttpBinding();
myBinding.Security.Mode = SecurityMode.Transport;
myBinding.Security.Transport.ClientCredentialType =
    HttpClientCredentialType.Basic;

// Create the endpoint address. Note that the machine name 
// must match the subject or DNS field of the X.509 certificate
// used to authenticate the service. 
EndpointAddress ea = new
    EndpointAddress("https://machineName/Calculator");

// Create the client. The code for the calculator 
// client is not shown here. See the sample applications
// for examples of the calculator code.
CalculatorClient cc =
    new CalculatorClient(myBinding, ea);
// The client must provide a user name and password. The code
// to return the user name and password is not shown here. Use
// a database to store the user name and passwords, or use the 
// ASP.NET Membership provider database.
cc.ClientCredentials.UserName.UserName = ReturnUsername();
cc.ClientCredentials.UserName.Password = ReturnPassword();
try
{
    // Begin using the client.
    cc.Open();
    Console.WriteLine(cc.Add(100, 11));
    Console.ReadLine();

    // Close the client.
    cc.Close();
}

構成

次のコードは、クライアントの構成を示しています。

ms733775.note(ja-jp,VS.100).gif注 :
構成を使用してユーザー名とパスワードを設定することはできません。ここに示した構成には、ユーザー名とパスワードを設定するためのコードを補う必要があります。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ICalculator" >
          <security mode="Transport">
            <transport clientCredentialType="Basic" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://machineName/Calculator" 
                binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_ICalculator" 
                contract="ICalculator"
                name="WSHttpBinding_ICalculator" />
    </client>
  </system.serviceModel>
</configuration>

参照

処理手順

方法 : SSL 証明書を使用してポートを構成する

リファレンス

ClientCredentials
UserNamePasswordClientCredential

概念

証明書の使用
セキュリティの概要

その他のリソース

ClientCredentials
Windows Server AppFabric のセキュリティ モデル