トランスポート セキュリティを使用する WSHttpBinding クラスを使用してセルフホステッド Windows Communication Foundation (WCF) サービスを作成する場合は、X.509 証明書を使用してポートを構成する必要もあります。 セルフホステッド サービスを作成していない場合は、インターネット インフォメーション サービス (IIS) でサービスをホストできます。 詳細については、「 HTTP トランスポート セキュリティ」を参照してください。
ポートを構成するには、使用するツールは、コンピューターで実行されているオペレーティング システムによって異なります。
Windows Server 2003 を実行している場合は、HttpCfg.exe ツールを使用します。 Windows Server 2003 では、このツールがインストールされています。 詳細については、「 Httpcfg の概要」を参照してください。 Windows サポート ツールのドキュメントでは、Httpcfg.exe ツールの構文について説明します。
Windows Vista を実行している場合は、既にインストールされている Netsh.exe ツールを使用します。
注
コンピューターに格納されている証明書を変更するには、管理者特権が必要です。
ポートの構成方法を決定する
Windows Server 2003 または Windows XP では、次の例に示すように、 クエリ と ssl スイッチを使用して、HttpCfg.exe ツールを使用して現在のポート構成を表示します。
httpcfg query ssl
Windows Vista では、次の例に示すように、Netsh.exe ツールを使用して現在のポート構成を表示します。
netsh http show sslcert
証明書の拇印を取得する
証明書 MMC スナップインを使用して、クライアント認証の目的を持つ X.509 証明書を見つけます。 詳細については、「 方法: MMC スナップインを使用して証明書を表示する」を参照してください。
証明書の拇印にアクセスします。 詳細については、「方法: 証明書のサムプリントを取得する」を参照してください。
証明書の拇印をメモ帳などのテキスト エディターにコピーします。
16 進文字の間のすべてのスペースを削除します。 これを実現する 1 つの方法は、テキスト エディターの検索と置換機能を使用し、各スペースを null 文字に置き換える方法です。
SSL 証明書をポート番号にバインドする
Windows Server 2003 または Windows XP では、Secure Sockets Layer (SSL) ストアの "set" モードで HttpCfg.exe ツールを使用して、証明書をポート番号にバインドします。 このツールでは、次の例に示すように、拇印を使用して証明書を識別します。
httpcfg set ssl -i 0.0.0.0:8012 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6
i スイッチには
IP
:port
の構文があり、コンピューターのポート 8012 に証明書を設定するようにツールに指示します。 必要に応じて、数値の前にある 4 つのゼロを、コンピューターの実際の IP アドレスに置き換えることができます。h スイッチは、証明書の拇印を指定します。
Windows Vista では、次の例に示すように、Netsh.exe ツールを使用します。
netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00001111-aaaa-2222-bbbb-3333cccc4444}
certhash パラメーターは、証明書の拇印を指定します。
ipport パラメーターは、IP アドレスとポートを指定し、Httpcfg.exe ツールの -i スイッチと同様に機能します。
appid パラメーターは、所有しているアプリケーションを識別するために使用できる GUID です。
SSL 証明書をポート番号にバインドし、クライアント証明書をサポートする
Windows Server 2003 または Windows XP では、トランスポート層で X.509 証明書で認証するクライアントをサポートするには、前の手順に従いますが、次の例に示すように、HttpCfg.exeに追加のコマンド ライン パラメーターを渡します。
httpcfg set ssl -i 0.0.0.0:8012 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6 -f 2
f スイッチには、
n
の構文があります。n は 1 から 7 の数値です。 前の例に示すように、値 2 を指定すると、トランスポート層でクライアント証明書が有効になります。 値 3 を指定すると、クライアント証明書が有効になり、それらの証明書が Windows アカウントにマップされます。 その他の値の動作については、HttpCfg.exe ヘルプを参照してください。Windows Vista では、トランスポート層で X.509 証明書を使用して認証するクライアントをサポートするには、前の手順に従いますが、次の例に示すように追加のパラメーターを使用します。
netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00001111-aaaa-2222-bbbb-3333cccc4444} clientcertnegotiation=enable
ポート番号から SSL 証明書を削除する
HttpCfg.exe または Netsh.exe ツールを使用して、コンピューター上のすべてのバインドのポートと拇印を確認します。 情報をディスクに出力するには、次の例に示すように、リダイレクト文字 ">" を使用します。
httpcfg query ssl>myMachinePorts.txt
Windows Server 2003 または Windows XP では、 削除 キーワードと ssl キーワードで HttpCfg.exe ツールを使用します。 i スイッチを使用して
IP
:port
番号を指定し、-h スイッチを使用して拇印を指定します。httpcfg delete ssl -i 0.0.0.0:8005 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6
Windows Vista では、次の例に示すように、Netsh.exe ツールを使用します。
Netsh http delete sslcert ipport=0.0.0.0:8005
例
次のコードは、セキュリティをトランスポートするために設定された WSHttpBinding クラスを使用して、セルフホステッド サービスを作成する方法を示しています。 アプリケーションを作成するときは、アドレスにポート番号を指定します。
// This string uses a function to prepend the computer name at run time.
string addressHttp = String.Format(
"http://{0}:8080/Calculator",
System.Net.Dns.GetHostEntry("").HostName);
WSHttpBinding b = new WSHttpBinding();
b.Security.Mode = SecurityMode.Transport;
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
// You must create an array of URI objects to have a base address.
Uri a = new Uri(addressHttp);
Uri[] baseAddresses = new Uri[] { a };
// Create the ServiceHost. The service type (Calculator) is not
// shown here.
ServiceHost sh = new ServiceHost(typeof(Calculator), baseAddresses);
// Add an endpoint to the service. Insert the thumbprint of an X.509
// certificate found on your computer.
Type c = typeof(ICalculator);
sh.AddServiceEndpoint(c, b, "MyCalculator");
sh.Credentials.ServiceCertificate.SetCertificate(
StoreLocation.LocalMachine,
StoreName.My,
X509FindType.FindBySubjectName,
"contoso.com");
// This next line is optional. It specifies that the client's certificate
// does not have to be issued by a trusted authority, but can be issued
// by a peer if it is in the Trusted People store. Do not use this setting
// for production code. The default is PeerTrust, which specifies that
// the certificate must originate from a trusted certificate authority.
// sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
// X509CertificateValidationMode.PeerOrChainTrust;
try
{
sh.Open();
string address = sh.Description.Endpoints[0].ListenUri.AbsoluteUri;
Console.WriteLine($"Listening @ {address}");
Console.WriteLine("Press enter to close the service");
Console.ReadLine();
sh.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine($"A communication error occurred: {ce.Message}");
Console.WriteLine();
}
catch (System.Exception exc)
{
Console.WriteLine($"An unforeseen error occurred: {exc.Message}");
Console.ReadLine();
}
' This string uses a function to prepend the computer name at run time.
Dim addressHttp As String = String.Format("http://{0}:8080/Calculator", _
System.Net.Dns.GetHostEntry("").HostName)
Dim b As New WSHttpBinding()
b.Security.Mode = SecurityMode.Transport
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate
' You must create an array of URI objects to have a base address.
Dim a As New Uri(addressHttp)
Dim baseAddresses() As Uri = {a}
' Create the ServiceHost. The service type (Calculator) is not
' shown here.
Dim sh As New ServiceHost(GetType(Calculator), baseAddresses)
' Add an endpoint to the service. Insert the thumbprint of an X.509
' certificate found on your computer.
Dim c As Type = GetType(ICalculator)
sh.AddServiceEndpoint(c, b, "MyCalculator")
sh.Credentials.ServiceCertificate.SetCertificate( _
StoreLocation.LocalMachine, _
StoreName.My, _
X509FindType.FindBySubjectName, _
"contoso.com")
' This next line is optional. It specifies that the client's certificate
' does not have to be issued by a trusted authority, but can be issued
' by a peer if it is in the Trusted People store. Do not use this setting
' for production code. The default is PeerTrust, which specifies that
' the certificate must originate from a trusted certificate authority.
' sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
' X509CertificateValidationMode.PeerOrChainTrust
Try
sh.Open()
Dim address As String = sh.Description.Endpoints(0).ListenUri.AbsoluteUri
Console.WriteLine("Listening @ {0}", address)
Console.WriteLine("Press enter to close the service")
Console.ReadLine()
sh.Close()
Catch ce As CommunicationException
Console.WriteLine("A communication error occurred: {0}", ce.Message)
Console.WriteLine()
Catch exc As System.Exception
Console.WriteLine("An unforeseen error occurred: {0}", exc.Message)
Console.ReadLine()
End Try