Question

I found a lot of answers how to set httplistener up to use HTTPS, but each one solution requires the use of command line. I guess that is the fastest way to do it but I would like to write C# class to handle this.

In old solution I used webserver class (found somewhere on Internet, I don't remember exact name) which allowed to add certificate in that way:

webserver.Certificate = new X509Certificate2("MyCert.pfx", "MyPassword");

Is there way to achieve this with httplistener? From code obviously.

Regards.

Was it helpful?

Solution

You can load a certificate with:

X509Certificate cert = new X509Certificate2("MyCert.pfx");

And then install it:

X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
if (!store.Certificates.Contains(cert))
{
    store.Add(cert);
}
store.Close();

Of course you might have to change the store name or location for your particular app.

For running the netsh command, you could look into creating and running a process (i.e. Process.Start) and run netsh.exe. Otherwise you have to mess with the Win32 HttpSetServiceConfiguration function, or the .NET equivalent if there is one.

You might find this codebox article useful: http://dotnetcodebox.blogspot.com/2012/01/how-to-work-with-ssl-certificate.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top