.netx509certificatehttplistener

HttpListener class with HTTPS support


There seems to be a lot of confusing, sometimes conflicting, information with regards to making a .NET HttpListener HTTPS capable. My understanding is as follows:

Is my understanding above correct? If not, please educate me.

Regarding X.509 certificates, my understanding is:

Once I get the certificate into the trusted store, I need to bind it to the TCP port. I am on Windows 7: should I be using httpcfg or netsh?


Solution

  • I did a bunch of homework and got this working. The steps to add SSL support for an .NET HttpListener are:

    1. Update C# application code to include the https prefix. Example:

      String[] prefixes = { "http://*:8089/","https://*:8443/" };
      

      That's it from the code aspect.

    2. For the certificate side of things, using the Windows SDK command console or Visual Studio Professional command console

      • Use makecert.exe to create a certificate authority. Example:

        makecert -n "CN=vMargeCA" -r -sv vMargeCA.pvk vMargeCA.cer
        
      • Use makecert.exe to create an SSL certificate

        makecert -sk vMargeSignedByCA -iv vMargeCA.pvk -n "CN=vMargeSignedByCA" -ic vMargeCA.cer vMargeSignedByCA.cer -sr localmachine -ss My
        
      • Use MMC GUI to install CA in Trusted Authority store

      • Use MMC GUI to install an SSL certificate in Personal store
      • Bind certificate to IP address:port and application. Example:

        netsh http add sslcert ipport=0.0.0.0:8443 certhash=585947f104b5bce53239f02d1c6fed06832f47dc appid={df8c8073-5a4b-4810-b469-5975a9c95230}
        

        The certhash is the thumbprint from your SSL certificate. You can find this using mmc. The appid is found in Visual Studio...usually in assembly.cs, look for the GUID value.

    There may be other ways to accomplish the above, but this worked for me.