statianzo / Fleck

C# Websocket Implementation
MIT License
2.25k stars 583 forks source link

Using Fleck in .NET core app with SSL certificate #297

Open captain-bugs opened 3 years ago

captain-bugs commented 3 years ago

I have already used this library with SSL in .NET Framework. But I'm planing to use in my .NET core app and I was wondering how can I use SSL (.pem format) certificate since I want to run the application on a Linux machine. Earlier I converted the SSL certificate from .pem to .pfx. Will it still work on Linux machines if i used the .pfx format ?

AdrianBathurst commented 3 years ago

Not sure, but probably not. I had this running on on Linux/.NET Framework (Mono) a long time ago. Though I was running this with SSL being stopped at Nginx, so SSL from client to Nginx, non SSL from Nginx to socket server. "Reverse Proxy" may be a route to tackle if you have issues.

RobertoMachorro commented 3 years ago

Try this and see if it works for you:

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout certificate.key -out certificate.crt
openssl pkcs12 -export -out certificate.pfx -inkey certificate.key -in certificate.crt
openssl pkcs12 -in certificate.pfx -out certificate.pem -cacerts

I use PFX with Fleck, though:

var address = String.Format("wss://0.0.0.0:{0}", this._port);
var server = new WebSocketServer(address);
var certpath = System.IO.Path.Combine(Environment.CurrentDirectory, "certificate.pfx");
server.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(certpath, String.Empty);