方法 : クライアントの資格情報の値を指定する

Windows Communication Foundation (WCF) では、サービスに対するクライアントの認証方法を指定できます。たとえば、証明書を使用してクライアントを認証するように指定できます。

クライアント資格情報の種類を特定するには

  1. サービスのメタデータ エンドポイントからメタデータを取得します。一般的に、メタデータは、選択したプログラミング言語 (既定は Visual C#) のクライアント コードおよび XML 構成ファイルという 2 つのファイルで構成されています。メタデータは、クライアント コードおよびクライアント構成を返す Svcutil.exe ツールを使用して取得できます。詳細については、「メタデータを取得する」および「ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe)」を参照してください。

  2. XML 構成ファイルを開きます。Svcutil.exe ツールを使用する場合、ファイルの既定の名前は、Output.config です。

  3. mode 属性のある <security> 要素 (<security mode = MessageOrTransport**>**) を見つけます。ここで、MessageOrTransport は、セキュリティ モードの 1 つに設定されています。

  4. mode 値に一致する子要素を見つけます。たとえば、mode が Message に設定されている場合、<security> 要素に含まれる <message> 要素を見つけます。

  5. clientCredentialType 属性に割り当てられている値に注意します。実際の値は、使用されているモード (トランスポートまたはメッセージ) に依存します。

次の XML コードは、メッセージ セキュリティを使用し、クライアントの認証に証明書を要求するクライアントの構成を示します。

<security mode="Message">
    <transport clientCredentialType="Windows" proxyCredentialType="None"
        realm="" />
     <message clientCredentialType="Certificate" negotiateServiceCredential="true"
    algorithmSuite="Default" establishSecurityContext="true" />
</security>

例: クライアント資格情報としての証明書による TCP トランスポート モード

この例では、セキュリティ モードを "Transport (トランスポート)" モードに設定し、クライアント資格情報の値を X.509 証明書に設定します。次の手順では、クライアントでクライアント資格情報の値をコードと構成を使用して設定する方法を示します。ここでは、サービスからメタデータ (コードと構成) を返すために ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) を使用していることを前提としています。詳細については、次のトピックを参照してください。 方法 : Windows Communication Foundation クライアントを作成する.

クライアントでクライアント資格情報の値をコードによって指定するには

  1. ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) を使用して、サービスからコードと構成を生成します。

  2. 生成されたコードを使用して、WCF クライアントのインスタンスを作成します。

  3. クライアント クラスで、ClientBase クラスの ClientCredentials プロパティを適切な値に設定します。この例では、X509CertificateInitiatorClientCredential クラスの SetCertificate メソッドを使用して、このプロパティを X.509 証明書に設定します。

    ' Create a binding using Transport and a certificate.
    Dim b As New NetTcpBinding()
    b.Security.Mode = SecurityMode.Transport
    b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate
    
    ' Create an EndPointAddress.
    Dim ea As New EndpointAddress("net.tcp://localHost:8036/Calculator/MyCalculator")
    
    ' Create the client.
    Dim cc As New CalculatorClient(b, ea)
    
    ' Set the certificate for the client.
    cc.ClientCredentials.ClientCertificate.SetCertificate( _
    StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "cohowinery.com")
    Try
        cc.Open()
        ' Begin using the client.
        Console.WriteLine(cc.Divide(1001, 2))
        cc.Close()
    Catch adExc As AddressAccessDeniedException
        Console.WriteLine(adExc.Message)
        Console.ReadLine()
    Catch exc As System.Exception
        Console.WriteLine(exc.Message)
        Console.ReadLine()
    End Try
    
    // Create a binding using Transport and a certificate.
    NetTcpBinding b = new NetTcpBinding();
    b.Security.Mode = SecurityMode.Transport;
    b.Security.Transport.ClientCredentialType = 
        TcpClientCredentialType.Certificate;
    
    // Create an EndPointAddress.
    EndpointAddress ea = new EndpointAddress(
        "net.tcp://localHost:8036/Calculator/MyCalculator");
    
    // Create the client.
    CalculatorClient cc = new CalculatorClient(b, ea);
    
    // Set the certificate for the client.
    cc.ClientCredentials.ClientCertificate.SetCertificate(
        StoreLocation.LocalMachine,
        StoreName.My,
        X509FindType.FindBySubjectName,
        "cohowinery.com");
    try
    {
        cc.Open();
        // Begin using the client.
        Console.WriteLine(cc.Divide(1001, 2));
        cc.Close();
    }
    catch (AddressAccessDeniedException adExc)
    {
        Console.WriteLine(adExc.Message);
        Console.ReadLine();
    }
    catch (System.Exception exc)
    {
        Console.WriteLine(exc.Message);
        Console.ReadLine();
    }          
    

    X509FindType クラスの任意の列挙体を使用できます。ここでは、有効期限が切れて証明書が変更された場合に備えて、サブジェクト名を使用しています。サブジェクト名を使用することにより、インフラストラクチャは証明書を再検索できます。

クライアントでクライアント資格情報の値を構成によって指定するには

  1. <behavior> of <endpointBehaviors> <behaviors> 要素を 要素に追加します。

  2. <clientCredentials> <behaviors> 要素を 要素に追加します。name 属性 (必須) を適切な値に必ず設定してください。

  3. <clientCertificate> of <serviceCredentials> <clientCredentials> 要素を 要素に追加します。

  4. 次のコードに示すように、storeLocationstoreNamex509FindType、および findValue の各属性を適切な値に設定します。証明書詳細情報、「証明書の使用」を参照してください。

    <behaviors>
       <endpointBehaviors>
          <behavior name="endpointCredentialBehavior">
            <clientCredentials>
              <clientCertificate findValue="Contoso.com" 
                                 storeLocation="LocalMachine"
                                 storeName="TrustedPeople"
                                 x509FindType="FindBySubjectName" />
            </clientCredentials>
          </behavior>
       </endpointBehaviors>
    </behaviors>
    
  5. クライアントを構成するときは、次のコードに示すように、<endpoint> 要素の behaviorConfiguration 属性を設定して動作を指定します。エンドポイント要素は <client> 要素の子です。また、bindingConfiguration 属性をクライアントのバインディングに設定することにより、バインディング構成の名前を指定します。生成された構成ファイルを使用している場合は、バインディングの名前は自動的に生成されます。この例では、名前は "tcpBindingWithCredential" です。

    <client>
      <endpoint name =""
                address="net.tcp://contoso.com:8036/aloha"
                binding="netTcpBinding"
                bindingConfiguration="tcpBindingWithCredential"
                behaviorConfiguration="endpointCredentialBehavior" />
    </client>
    

参照

処理手順

方法 : Windows Communication Foundation クライアントを作成する

リファレンス

NetTcpBinding
SetCertificate
X509CertificateRecipientServiceCredential
ClientBase
X509CertificateInitiatorClientCredential

概念

WCF セキュリティのプログラミング
資格情報の種類の選択
ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe)
証明書の使用

その他のリソース

<netTcpBinding>
<security> of <netTcpBinding>
<message> element of <netTcpBinding>
<behavior> of <endpointBehaviors>
<behaviors>
<clientCertificate> of <serviceCredentials>
<clientCredentials>