サービスは、Windows Communication Foundation (WCF) を使用して、サービスに対するクライアントの認証方法を指定できます。 たとえば、サービスは、クライアントを証明書で認証することを規定できます。
クライアント資格情報の種類を確認するには
サービスのメタデータ エンドポイントからメタデータを取得します。 通常、メタデータは、選択したプログラミング言語のクライアント コード (既定値は Visual C#) と XML 構成ファイルの 2 つのファイルで構成されます。 メタデータを取得する方法の 1 つは、Svcutil.exe ツールを使用してクライアント コードとクライアント構成を返す方法です。 詳細については、「 メタデータ と ServiceModel メタデータ ユーティリティ ツールの取得 (Svcutil.exe)」を参照してください。
XML 構成ファイルを開きます。 Svcutil.exe ツールを使用する場合、ファイルの既定の名前は Output.config。
<セキュリティ>要素で
mode属性 (<モード=MessageOrTransportがセキュリティモードのいずれかに設定されている) を見つけます。モード値に一致する子要素を検索します。 たとえば、モードが Message に設定されている場合は、<security> 要素に含まれる <message> 要素を見つけます。
clientCredentialType属性に割り当てられた値に注意してください。 実際の値は、使用されるモード(トランスポートまたはメッセージ)によって異なります。
次の XML コードは、メッセージ セキュリティを使用し、クライアントの認証に証明書を要求するクライアントの構成を示しています。
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Certificate" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
例: クライアント資格情報としての証明書を使用した TCP トランスポート モード
次の使用例は、セキュリティ モードをトランスポート モードに設定し、クライアント資格情報の値を X.509 証明書に設定します。 次の手順では、コードと構成でクライアントのクライアント資格情報の値を設定する方法を示します。 これは、 ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) を使用して、サービスからメタデータ (コードと構成) を返していることを前提としています。 詳細については、「 方法: クライアントを作成する」を参照してください。
コードでクライアントのクライアント資格情報の値を指定するには
ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) を使用して、サービスからコードと構成を生成します。
生成されたコードを使用して、WCF クライアントのインスタンスを作成します。
クライアント クラスで、ClientCredentials クラスの ClientBase<TChannel> プロパティを適切な値に設定します。 次の使用例は、SetCertificate クラスの X509CertificateInitiatorClientCredential メソッドを使用して、プロパティを X.509 証明書に設定します。
// 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(); }' 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 TryX509FindType クラスの任意の列挙体を使用できます。 サブジェクト名は、証明書が (有効期限が原因で) 変更された場合に使用されます。 サブジェクト名を使用すると、インフラストラクチャで証明書を再度検索できます。
構成でクライアントのクライアント資格情報の値を指定するには
<behavior> 要素を <behaviors> 要素に追加します。
<clientCredentials> 要素を <behaviors> 要素に追加します。 必要な
name属性を適切な値に設定してください。<clientCertificate> 要素を <clientCredentials> 要素に追加します。
次のコードに示すように、
storeLocation、storeName、x509FindType、findValueの各属性を適切な値に設定します。 証明書の詳細については、「証明書 の操作」を参照してください。<behaviors> <endpointBehaviors> <behavior name="endpointCredentialBehavior"> <clientCredentials> <clientCertificate findValue="Contoso.com" storeLocation="LocalMachine" storeName="TrustedPeople" x509FindType="FindBySubjectName" /> </clientCredentials> </behavior> </endpointBehaviors> </behaviors>クライアントを構成する場合は、次のコードに示すように、
behaviorConfiguration要素の<endpoint>属性を設定して動作を指定します。 endpoint 要素は、 <client> 要素の子です。 また、bindingConfiguration属性をクライアントのバインドに設定して、バインド構成の名前を指定します。 生成された構成ファイルを使用している場合、バインディングの名前が自動的に生成されます。 この例では、名前は"tcpBindingWithCredential"。<client> <endpoint name ="" address="net.tcp://contoso.com:8036/aloha" binding="netTcpBinding" bindingConfiguration="tcpBindingWithCredential" behaviorConfiguration="endpointCredentialBehavior" /> </client>
こちらも参照ください
- NetTcpBinding
- SetCertificate
- X509CertificateRecipientServiceCredential
- ClientBase<TChannel>
- X509CertificateInitiatorClientCredential
- WCF セキュリティのプログラミング
- 資格情報の種類の選択
- ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe)
- 証明書の操作
- 方法: クライアントを作成する
- <netTcpBinding>
- <安全>
- <メッセージ>
- <振舞い>
- <動作>
- <クライアント証明書>
- <clientCredentials>