Quando si crea un Windows Communication Foundation self-hosted che utilizza SSL, è necessario configurare una porta con certificato X.509. Quindi dopo aver creato il servizio ed averlo opportunamente configurato per utilizzare il protocollo SSL occorre:
- Utilizzare netsh per aggiungere una prenotazione dello spazio dei nomi per la porta da utilizzare (50001):
C:\Windows\system32>netsh http add urlacl url=https://+:50001/ user=EVERYONE
- Creare un certificato ROOT da utilizzare per firmare il sertificato del server:
makecert.exe -sk RootCA -sky signature -pe -n CN=localhost -r
-sr LocalMachine -ss Root MyRootCA.cer
- Creare il certificato del server:
makecert.exe -sk server -sky exchange -pe -n CN=localhost -ir LocalMachine -is Root
-ic MyRootCA.cer -sr LocalMachine -ss My MyTestCert.cer
- una volta crearo il sertificato del server occorre collagarlo alla porta utilizzata dal servizio, per farlo utilizziamo una piccola applicazione di utility:
X509Certificate2 certificate = new X509Certificate2("MyTestCert.cer");
Process netsh = new Process();
netsh.StartInfo.FileName =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86),
"netsh.exe");
netsh.StartInfo.Arguments =
String.Format("http add sslcert ipport=0.0.0.0:{0} certhash={1} appid={{{2}}}",
50001, certificate.Thumbprint, "49e33699-ca14-42cf-bd09-00675942b894");
netsh.Start();
netsh.WaitForExit();
in alternativa e' possibile utilizzare il comando:
Snippet
netsh http add sslcert ipport=0.0.0.0:50001 certhash=f97d7e0ca58e8bb1acccf46c0f42d9b8934cddec appid={49e33699-ca14-42cf-bd09-00675942b894}
dove certhash puo' essere ricavato dalla proprieta' Thumbprint del certificato (MyTestCert.cer in questo caso):
Snippet
f97d7e0ca58e8bb1acccf46c0f42d9b8934cddec
mentre appid e' il guid dell'applicazione che ospita il servizio, ricavabile dal file AssemblyInfo.cs:
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("49e33699-ca14-42cf-bd09-00675942b894")]
- Quindi possiamo verificare il funzionamento del servizio WCF con protocollo SSL:
https://localhost:50001/WcfServiceHTTPSSelfHosted/GetData?value=44
che ritorna il seguente risultato
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">You entered: 44</string>
- Per visualizzare le porte prenotate:
Snippet
netsh http show urlacl
- Per eliminare un aprenotazione su una specifica porta:
Snippet
netsh http delete urlacl url=https://+:50001/
- Per eliminare un certificato:
Snippet
netsh http delete sslcert ipport=0.0.0.0:50001
- Per visualizzare i certificati collegati ad una porta:
Snippet
netsh http show sslcert ipport=0.0.0.0:50001
WcfServiceHTTPSSelfHosted.zip