方法: トランスポート セキュリティとメッセージ資格情報を使用する

トランスポートとメッセージの両方の資格情報を使用してサービスをセキュリティで保護する場合、Windows Communication Foundation (WCF) では、トランスポートとメッセージの両方のセキュリティ モードが最適に使用されます。 つまり、トランスポート層セキュリティでは整合性と機密性が提供され、メッセージ層セキュリティでは、厳密なトランスポート セキュリティ機構では実現できないさまざまな資格情報が提供されます。 ここでは、WSHttpBinding バインディングと NetTcpBinding バインディングを使用して、メッセージ資格情報付きトランスポートを実装するための基本手順を示します。 セキュリティ モードの設定の詳細については、「方法: セキュリティ モードを設定する」を参照してください。

セキュリティ モードを TransportWithMessageCredential に設定した場合、トランスポート レベルのセキュリティを提供する実際の機構はトランスポートによって決まります。 この機構は、HTTP の場合は SSL (Secure Sockets Layer) over HTTP (HTTPS)、TCP の場合は SSL over TCP または Windows です。

トランスポートが HTTP (WSHttpBinding を使用) の場合は、トランスポート レベルのセキュリティが SSL over HTTP によって提供されます。 この場合、このトピックで後述するように、ポートにバインドされた SSL 証明書を使用してサービスをホストするコンピューターを構成する必要があります。

トランスポートが TCP (NetTcpBinding を使用) の場合、既定では、Windows セキュリティ または SSL over TCP によってトランスポート レベルのセキュリティが提供されます。 SSL over TCP を使用する場合は、このトピックで後述するように、SetCertificate メソッドを使用して証明書を指定する必要があります。

WSHttpBinding と証明書を使用してトランスポート セキュリティを提供するには (コードを使用する場合)

  1. HttpCfg.exe ツールを使用して、コンピューターの任意のポートに SSL 証明書をバインドします。 詳細については、「方法: SSL 証明書を使用してポートを構成する」を参照してください。

  2. WSHttpBinding クラスのインスタンスを作成し、Mode プロパティを TransportWithMessageCredential に設定します。

  3. ClientCredentialType プロパティに適切な値を設定します。 (詳細については、「資格情報の種類の選択」を参照してください。)次のコードでは、Certificate 値を使用しています。

  4. 適切なベース アドレスを持つ Uri クラスのインスタンスを作成します。 このアドレスでは、"HTTPS" スキームを使用し、コンピューターの実際の名前と SSL 証明書のバインド先のポート番号を含める必要があります。 (または、構成で基本アドレスを設定できます。)

  5. AddServiceEndpoint メソッドを使用してサービス エンドポイントを追加します。

  6. ServiceHost のインスタンスを作成し、Open メソッドを呼び出します。コードは次のようになります。

    WSHttpBinding b = new WSHttpBinding();
    b.Security.Mode = SecurityMode.TransportWithMessageCredential;
    b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
    
    // The SSL certificate is bound to port 8006 using the HttpCfg.exe tool.
    Uri httpsAddress = new Uri("https://localMachineName:8006/base");
    ServiceHost sh = new ServiceHost(typeof(Calculator), httpsAddress);
    sh.AddServiceEndpoint(typeof(ICalculator), b, "HttpsCalculator");
    sh.Open();
    Console.WriteLine("Listening");
    Console.ReadLine();
    
    Dim b As New WSHttpBinding()
    b.Security.Mode = SecurityMode.TransportWithMessageCredential
    b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate
    
    ' The SSL certificate is bound to port 8006 using the HttpCfg.exe tool.
    Dim httpsAddress As New Uri("https://localMachineName:8006/base")
    Dim sh As New ServiceHost(GetType(Calculator), httpsAddress)
    sh.AddServiceEndpoint(GetType(ICalculator), b, "HttpsCalculator")
    sh.Open()
    Console.WriteLine("Listening")
    Console.ReadLine()
    

NetTcpBinding と証明書を使用してトランスポート セキュリティを提供するには (コードを使用する場合)

  1. NetTcpBinding クラスのインスタンスを作成し、Mode プロパティを TransportWithMessageCredential に設定します。

  2. ClientCredentialType を適切な値に設定します。 次のコードでは、Certificate 値を使用しています。

  3. 適切なベース アドレスを持つ Uri クラスのインスタンスを作成します。 アドレスには "net.tcp" スキーマを使用する必要があることに注意してください。 (または、構成で基本アドレスを設定できます。)

  4. ServiceHost クラスのインスタンスを作成します。

  5. SetCertificate クラスの X509CertificateRecipientServiceCredential メソッドを使用して、サービスに X.509 資格情報を明示的に設定します。

  6. AddServiceEndpoint メソッドを使用してサービス エンドポイントを追加します。

  7. 次のコードに示すように、Open メソッドを呼び出します。

    NetTcpBinding b = new NetTcpBinding(SecurityMode.TransportWithMessageCredential);
    b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
    Uri netTcpAddress = new Uri("net.tcp://baseAddress");
    ServiceHost sh = new ServiceHost(typeof(Calculator), netTcpAddress);
    sh.Credentials.ServiceCertificate.SetCertificate(
        StoreLocation.LocalMachine, StoreName.My,
        X509FindType.FindByIssuerName, "Contoso.com");
    sh.AddServiceEndpoint(typeof(ICalculator), b, "TcpCalculator");
    sh.Open();
    Console.WriteLine("Listening");
    Console.ReadLine();
    
    Dim b As New NetTcpBinding(SecurityMode.TransportWithMessageCredential)
    b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate
    Dim netTcpAddress As New Uri("net.tcp://baseAddress")
    Dim sh As New ServiceHost(GetType(Calculator), netTcpAddress)
    sh.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByIssuerName, "Contoso.com")
    sh.AddServiceEndpoint(GetType(ICalculator), b, "TcpCalculator")
    sh.Open()
    Console.WriteLine("Listening")
    Console.ReadLine()
    

NetTcpBinding と Windows を使用してトランスポート セキュリティを提供するには (コードを使用する場合)

  1. NetTcpBinding クラスのインスタンスを作成し、Mode プロパティを TransportWithMessageCredential に設定します。

  2. トランスポート セキュリティが Windows を使用するように、ClientCredentialTypeWindows に設定します (これは、既定の設定です)。

  3. ClientCredentialType を適切な値に設定します。 次のコードでは、Certificate 値を使用しています。

  4. 適切なベース アドレスを持つ Uri クラスのインスタンスを作成します。 アドレスには "net.tcp" スキーマを使用する必要があることに注意してください。 (または、構成で基本アドレスを設定できます。)

  5. ServiceHost クラスのインスタンスを作成します。

  6. SetCertificate クラスの X509CertificateRecipientServiceCredential メソッドを使用して、サービスに X.509 資格情報を明示的に設定します。

  7. AddServiceEndpoint メソッドを使用してサービス エンドポイントを追加します。

  8. 次のコードに示すように、Open メソッドを呼び出します。

    NetTcpBinding b = new NetTcpBinding(SecurityMode.TransportWithMessageCredential);
    b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
    b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
    Uri netTcpAddress = new Uri("net.tcp://Tcp");
    ServiceHost sh = new ServiceHost(typeof(Calculator), netTcpAddress);
    sh.Credentials.ServiceCertificate.SetCertificate(
        StoreLocation.LocalMachine, StoreName.My,
        X509FindType.FindByIssuerName, "Contoso.com");
    sh.AddServiceEndpoint(typeof(ICalculator), b, "TcpCalculator");
    sh.Open();
    Console.WriteLine("Listening");
    Console.ReadLine();
    
    Dim b As New NetTcpBinding(SecurityMode.TransportWithMessageCredential)
    b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows
    b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate
    Dim netTcpAddress As New Uri("net.tcp://Tcp")
    Dim sh As New ServiceHost(GetType(Calculator), netTcpAddress)
    sh.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByIssuerName, "Contoso.com")
    sh.AddServiceEndpoint(GetType(ICalculator), b, "TcpCalculator")
    sh.Open()
    Console.WriteLine("Listening")
    Console.ReadLine()
    

構成の使用

WSHttpBinding を使用するには

  1. ポートにバインドされた SSL 証明書を使用してコンピューターを構成します (詳細については、「方法: SSL 証明書を使用してポートを構成する」を参照してください)。 この構成では、<transport> 要素値を設定する必要はありません。

  2. メッセージ レベルのセキュリティのクライアント資格情報の種類を指定します。 次の例では、<message> 要素の clientCredentialType 属性を UserName に設定しています。

    <wsHttpBinding>  
    <binding name="WsHttpBinding_ICalculator">  
            <security mode="TransportWithMessageCredential" >  
               <message clientCredentialType="UserName" />  
            </security>  
    </binding>  
    </wsHttpBinding>  
    

NetTcpBinding と証明書を使用してトランスポート セキュリティを提供するには

  1. SSL over TCP の場合、<behaviors> 要素内で証明書を明示的に指定する必要があります。 既定のストアの場所 (ローカル コンピューターの個人ストア) にある証明書を、発行者で指定する例を次に示します。

    <behaviors>  
     <serviceBehaviors>  
       <behavior name="mySvcBehavior">  
           <serviceCredentials>  
             <serviceCertificate findValue="contoso.com"  
                                 x509FindType="FindByIssuerName" />  
           </serviceCredentials>  
       </behavior>  
     </serviceBehaviors>  
    </behaviors>  
    
  2. <netTcpBinding> を bindings セクションに追加します。

  3. バインド要素を追加して、name 属性を適切な値に設定します。

  4. <security> 要素を追加し、mode 属性を TransportWithMessageCredential に設定します。

  5. <message> 要素を追加し、clientCredentialType 属性を適切な値に設定します。

    <bindings>  
    <netTcpBinding>  
      <binding name="myTcpBinding">  
        <security mode="TransportWithMessageCredential" >  
           <message clientCredentialType="Windows" />  
        </security>  
      </binding>  
    </netTcpBinding>  
    </bindings>  
    

NetTcpBinding と Windows を使用してトランスポート セキュリティを提供するには

  1. <netTcpBinding> を bindings セクションに追加します。

  2. <binding> 要素を追加し、name 属性に適切な値を設定します。

  3. <security> 要素を追加し、mode 属性を TransportWithMessageCredential に設定します。

  4. <transport> 要素を追加し、clientCredentialType 属性を Windows に設定します。

  5. <message> 要素を追加し、clientCredentialType 属性に適切な値を設定します。 次のコードは、値を証明書に設定します。

    <bindings>  
    <netTcpBinding>  
      <binding name="myTcpBinding">  
        <security mode="TransportWithMessageCredential" >  
           <transport clientCredentialType="Windows" />  
           <message clientCredentialType="Certificate" />  
        </security>  
      </binding>  
    </netTcpBinding>  
    </bindings>  
    

関連項目