Procedura: configurare una porta con un certificato SSL

Quando si crea un servizio Windows Communication Foundation (WCF) indipendente con la classe WSHttpBinding che usa la protezione del trasporto, è necessario configurare anche una porta con un certificato X.509. Se non si intende creare un servizio indipendente, è possibile ospitare il servizio in Internet Information Services (IIS). Per altre informazioni, vedere Sicurezza del trasporto HTTP.

Per configurare una porta, lo strumento da usare dipende dal sistema operativo eseguito nel computer.

Se viene eseguito Windows Server 2003, usare lo strumento HttpCfg.exe. In Windows Server 2003 questo strumento è installato. Per altre informazioni, vedere Panoramica su httpcfg. Nella documentazione sugli strumenti di supporto Windows viene illustrata la sintassi dello strumento Httpcfg.exe.

Se viene eseguito Windows Vista, usare lo strumento Netsh.exe già installato.

Nota

La modifica dei certificati archiviati nel computer richiede privilegi di amministrazione.

Determinare la configurazione delle porte

  1. In Windows Server 2003 o Windows XP utilizzare lo strumento HttpCfg.exe per visualizzare la configurazione corrente delle porte, utilizzando le opzioni query e ssl come illustrato nell'esempio seguente.

    httpcfg query ssl  
    
  2. In Windows Vista usare lo strumento Netsh.exe per visualizzare la configurazione corrente delle porte, come illustrato nell'esempio seguente.

    netsh http show sslcert  
    

Ottenere l'identificazione personale di un certificato

  1. Usare lo snap-in MMC Certificati per individuare un certificato X.509 che abbia come scopo previsto l'autenticazione client. Per altre informazioni, vedere Procedura: Visualizzare certificati con lo snap-in MMC.

  2. Accedere all'identificazione personale del certificato. Per altre informazioni, vedere Procedura: recuperare l'identificazione personale di un certificato.

  3. Copiare l'identificazione personale del certificato in un editor di testo, ad esempio Blocco note.

  4. Rimuovere tutti gli spazi tra i caratteri esadecimali. Per eseguire questa operazione, è possibile usare la funzionalità Trova e sostituisci dell'editor di testo per sostituire ogni spazio con un carattere null.

Associare un certificato SSL a un numero di porta

  1. In Windows Server 2003 o Windows XP, usare lo strumento HttpCfg.exe in modalità "impostazione" nell'archivio SSL (Secure Sockets Layer) per associare il certificato a un numero di porta. Lo strumento usa l'identificazione personale per identificare il certificato, come illustrato nell'esempio seguente.

    httpcfg set ssl -i 0.0.0.0:8012 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6  
    
    • L'opzione -i contiene la sintassi di IP:port e indica allo strumento di impostare il certificato sulla porta 8012 del computer. I quattro zero che precedono il numero possono essere anche sostituiti dall'indirizzo IP effettivo del computer.

    • L'opzione -h specifica l'identificazione digitale del certificato.

  2. In Windows Vista usare lo strumento Netsh.exe come illustrato nell'esempio seguente.

    netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF}
    
    • Il parametro certhash consente di specificare l'identificazione personale del certificato.

    • Il parametro ipport consente di specificare la porta e l'indirizzo IP e funzioni come l'opzione -i dello strumento Httpcfg.exe descritto.

    • Il parametro appid è un GUID che può essere utilizzato per identificare l'applicazione proprietaria.

Associare un certificato SSL a un numero di porta e ai certificati client supportati

  1. In Windows Server 2003 o Windows XP per supportare client che eseguono l'autenticazione con i certificati X.509 a livello di trasporto, eseguire la procedura precedente passando a HttpCfg.exe un parametro aggiuntivo della riga di comando, come illustrato nell'esempio seguente.

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

    L'opzione -f dispone della sintassi di n dove n è un numero compreso tra 1 e 7. Un valore di 2, come mostrato nell'esempio precedente, abilita certificati client a livello di trasporto. Il valore 3 attiva i certificati client e associa tali certificati a un account Windows. Per il comportamento di altri valori, vedere la Guida di HttpCfg.exe.

  2. In Windows Vista per supportare i client che eseguono l'autenticazione con i certificati X.509 a livello di trasporto, eseguire la procedura precedente, ma con un parametro aggiuntivo, come illustrato nell'esempio seguente.

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

Eliminare un certificato SSL da un numero di porta

  1. Usare lo strumento HttpCfg.exe o Netsh.exe per visualizzare le porte e le identificazioni personali di tutti i binding nel computer. Per stampare le informazioni su disco, usare il carattere di reindirizzamento ">", come mostrato nell'esempio seguente.

    httpcfg query ssl>myMachinePorts.txt  
    
  2. In Windows Server 2003 o Windows XP usare lo strumento HttpCfg.exe con le parole chiave delete e ssl. Utilizzare l'opzione -i per specificare il numero IP:port e l'opzione -h per specificare l'identificazione personale.

    httpcfg delete ssl -i 0.0.0.0:8005 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6  
    
  3. In Windows Vista usare lo strumento Netsh.exe come illustrato nell'esempio seguente.

    Netsh http delete sslcert ipport=0.0.0.0:8005  
    

Esempio

Nel codice seguente viene illustrato come creare un servizio indipendente usando la classe WSHttpBinding per la protezione del trasporto. Quando si crea un'applicazione, specificare il numero di porta nell'indirizzo.

// 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

Vedi anche