unosquare / embedio

A tiny, cross-platform, module based web server for .NET
http://unosquare.github.io/embedio
Other
1.47k stars 176 forks source link

HTTPS is not working as expected #462

Closed miriambron closed 4 years ago

miriambron commented 4 years ago

While trying to use https server as described in the HttpsTest. It is not working: In the log I see 12:37:22.021 DBG >> [NetSh] SSL Certificate bindings: 12:37:22.027 DBG >> [NetSh] ------------------------- 12:37:22.027 DBG >> [NetSh] The system cannot find the file specified.

When trying to add certificate , and play with the other parameters, nothing didn't work.

rdeago commented 4 years ago

Hello @miriambron, thanks for using EmbedIO!

In order to be able to help you, we need some more information:

miriambron commented 4 years ago

Hi, Thanks for quick response! I'm using Windows 10 . .Net framework Version=v4.7.1

the Code:

        var url = "https://+:9697";

        var options = new WebServerOptions()
            .WithUrlPrefix(url)
            .WithAutoLoadCertificate()
            .WithMode(HttpListenerMode.EmbedIO);
        var webServer = new WebServer(options);
        webServer.OnAny(ctx => ctx.SendStringAsync("sssss", MimeType.PlainText, Encoding.UTF8));

        var cts = new CancellationTokenSource();
        var task = webServer.RunAsync(cts.Token);

        // Wait for any key to be pressed before disposing of our web server.
        // In a service we'd manage the lifecycle of of our web server using
        // something like a BackgroundWorker or a ManualResetEvent.
        Console.ReadKey(true);
        cts.Cancel();
        try
        {
            task.Wait();
        }
        catch (AggregateException)
        {
            // We'd also actually verify the exception cause was that the task
            // was cancelled.
            webServer.Dispose();
        }

When changing to http , and removing the WithAutoLoadCertificate option , it works.

Thanks in advance!

rdeago commented 4 years ago

Looks like there is no SSL certificate in your system that EmbedIO can use.

EmbedIO can (to some extent, and only under Windows) automatically load a SSL certificate, but it cannot generate one for you.

@geoperez any thoughts?

miriambron commented 4 years ago

Okay, Thanks! If I want to use another options , I see in the documentation:

You need to create a WebServerOptions instance with the path to a PFX file and the AutoRegister flag on

But I can't see where I can specify the path , in the options

miriambron commented 4 years ago

Looks like there is no SSL certificate in your system that EmbedIO can use.

EmbedIO can (to some extent, and only under Windows) automatically load a SSL certificate, but it cannot generate one for you.

@geoperez any thoughts?

After I try to add Certificate to store, and I don't see anymore the " [NetSh] The system cannot find the file specified." , Still , I can connect to the https server

rdeago commented 4 years ago

Apparently the information you found is outdated. Not your fault.

Unfortunately I'm not an expert at all when it comes to HTTPS and certificates, but my google-fu is strong. :wink: Try this:

    .WithCertificate(new X509Certificate2(PATH_OF_YOUR_CERTIFICATE))
    .WithAutoRegisterCertificate()
miriambron commented 4 years ago

Apparently the information you found is outdated. Not your fault.

Unfortunately I'm not an expert at all when it comes to HTTPS and certificates, but my google-fu is strong. 😉 Try this:

    .WithCertificate(new X509Certificate2(PATH_OF_YOUR_CERTIFICATE))
    .WithAutoRegisterCertificate()

This is taken from Here: https://github.com/unosquare/embedio#support-for-ssl When I tried it , I'm getting

System.InvalidOperationException: 'NetSh error: SSL Certificate add failed, Error: 1312 A specified logon session does not exist. It may already have been terminated.

I tried to play with many options I see , nothing works.

miriambron commented 4 years ago

Okay , when I used Self-Sign certificate , like in : https://stackoverflow.com/questions/13806299/how-can-i-create-a-self-signed-certificate-using-c

It works.

Thanks!

rdeago commented 4 years ago

Glad you have made it. Happy HTTPS serving! 😃

miriambron commented 4 years ago

Glad you have made it. Happy HTTPS serving! 😃

@rdeago thanks!