Udostępnij za pośrednictwem


Instrukcje: Konfigurowanie portu z certyfikatem SSL

Podczas tworzenia własnej usługi Windows Communication Foundation (WCF) z klasą korzystającą z WSHttpBinding zabezpieczeń transportu należy również skonfigurować port z certyfikatem X.509. Jeśli nie tworzysz samoobsługowej usługi, możesz hostować usługę w usługach Internet Information Services (IIS). Aby uzyskać więcej informacji, zobacz Zabezpieczenia transportu HTTP.

Aby skonfigurować port, używane narzędzie zależy od systemu operacyjnego uruchomionego na komputerze.

Jeśli korzystasz z systemu Windows Server 2003, użyj narzędzia HttpCfg.exe. W systemie Windows Server 2003 to narzędzie jest zainstalowane. Aby uzyskać więcej informacji, zobacz Httpcfg Overview (Omówienie protokołu Httpcfg). W dokumentacji narzędzi pomocy technicznej systemu Windows opisano składnię narzędzia Httpcfg.exe.

Jeśli korzystasz z systemu Windows Vista, użyj narzędzia Netsh.exe, które jest już zainstalowane.

Uwaga

Modyfikowanie certyfikatów przechowywanych na komputerze wymaga uprawnień administracyjnych.

Określanie sposobu konfigurowania portów

  1. W systemie Windows Server 2003 lub Windows XP użyj narzędzia HttpCfg.exe, aby wyświetlić bieżącą konfigurację portu przy użyciu zapytań i przełączników ssl , jak pokazano w poniższym przykładzie.

    httpcfg query ssl  
    
  2. W systemie Windows Vista użyj narzędzia Netsh.exe, aby wyświetlić bieżącą konfigurację portu, jak pokazano w poniższym przykładzie.

    netsh http show sslcert  
    

Pobieranie odcisku palca certyfikatu

  1. Użyj przystawki MMC Certyfikaty, aby znaleźć certyfikat X.509, który ma zamierzony cel uwierzytelniania klienta. Aby uzyskać więcej informacji, zobacz How to: View Certificates with the MMC Snap-in (Instrukcje: wyświetlanie certyfikatów za pomocą przystawki MMC).

  2. Uzyskaj dostęp do odcisku palca certyfikatu. Aby uzyskać więcej informacji, zobacz Instrukcje: Pobieranie odcisku palca certyfikatu.

  3. Skopiuj odcisk palca certyfikatu do edytora tekstów, takiego jak Notatnik.

  4. Usuń wszystkie spacje między znakami szesnastkowymi. Jednym ze sposobów osiągnięcia tego celu jest użycie funkcji find-and-replace edytora tekstów i zastąpienie każdego miejsca znakiem null.

Wiązanie certyfikatu SSL z numerem portu

  1. W systemie Windows Server 2003 lub Windows XP użyj narzędzia HttpCfg.exe w trybie "set" w magazynie Secure Sockets Layer (SSL), aby powiązać certyfikat z numerem portu. Narzędzie używa odcisku palca do identyfikowania certyfikatu, jak pokazano w poniższym przykładzie.

    httpcfg set ssl -i 0.0.0.0:8012 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6  
    
    • Przełącznik -i ma składnię IP:port i nakazuje narzędziu ustawienie certyfikatu na port 8012 komputera. Opcjonalnie cztery zera, które poprzedzają liczbę, można również zastąpić rzeczywistym adresem IP komputera.

    • Przełącznik -h określa odcisk palca certyfikatu.

  2. W systemie Windows Vista użyj narzędzia Netsh.exe, jak pokazano w poniższym przykładzie.

    netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF}
    
    • Parametr certhash określa odcisk palca certyfikatu.

    • Parametr ipport określa adres IP i port, a funkcje podobnie jak przełącznik -i opisanego narzędzia Httpcfg.exe.

    • Parametr appid jest identyfikatorem GUID, który może służyć do identyfikowania aplikacji będącej właścicielem.

Wiązanie certyfikatu SSL z numerem portu i obsługą certyfikatów klienta

  1. W systemie Windows Server 2003 lub Windows XP, aby obsługiwać klientów, którzy uwierzytelniają się przy użyciu certyfikatów X.509 w warstwie transportu, wykonaj poprzednią procedurę, ale przekaż dodatkowy parametr wiersza polecenia do HttpCfg.exe, jak pokazano w poniższym przykładzie.

    httpcfg set ssl -i 0.0.0.0:8012 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6 -f 2  
    

    Przełącznik -f ma składnię n , gdzie n jest liczbą z zakresu od 1 do 7. Wartość 2, jak pokazano w poprzednim przykładzie, umożliwia certyfikaty klienta w warstwie transportu. Wartość 3 umożliwia certyfikaty klienta i mapuje te certyfikaty na konto systemu Windows. Zobacz HttpCfg.exe Pomoc, aby uzyskać informacje o zachowaniu innych wartości.

  2. W systemie Windows Vista, aby obsługiwać klientów, którzy uwierzytelniają się przy użyciu certyfikatów X.509 w warstwie transportu, wykonaj poprzednią procedurę, ale z dodatkowym parametrem, jak pokazano w poniższym przykładzie.

    netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF} clientcertnegotiation=enable  
    

Usuwanie certyfikatu SSL z numeru portu

  1. Użyj narzędzia HttpCfg.exe lub Netsh.exe, aby wyświetlić porty i odciski palca wszystkich powiązań na komputerze. Aby wydrukować informacje na dysku, użyj znaku przekierowania ">", jak pokazano w poniższym przykładzie.

    httpcfg query ssl>myMachinePorts.txt  
    
  2. W systemie Windows Server 2003 lub Windows XP użyj narzędzia HttpCfg.exe ze słowami kluczowymi usuwania i ssl . Użyj przełącznika -i , aby określić IPliczbę :port i przełącznik -h , aby określić odcisk palca.

    httpcfg delete ssl -i 0.0.0.0:8005 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6  
    
  3. W systemie Windows Vista użyj narzędzia Netsh.exe, jak pokazano w poniższym przykładzie.

    Netsh http delete sslcert ipport=0.0.0.0:8005  
    

Przykład

Poniższy kod pokazuje, jak utworzyć usługę self-hosted przy użyciu klasy ustawionej WSHttpBinding na transport zabezpieczeń. Podczas tworzenia aplikacji określ numer portu w adresie.

// 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 @ {0}", address);
    Console.WriteLine("Press enter to close the service");
    Console.ReadLine();
    sh.Close();
}
catch (CommunicationException ce)
{
    Console.WriteLine("A communication error occurred: {0}", ce.Message);
    Console.WriteLine();
}
catch (System.Exception exc)
{
    Console.WriteLine("An unforeseen error occurred: {0}", 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

Zobacz też