簡體   English   中英

C#HttpListener在SSL上完全失敗

[英]C# HttpListener total failure on SSL

好的,如果我正在運行此簡化的代碼片段,則該程序將嚴重崩潰,甚至不會引發異常。

所以這是我之前做過的事情:

  • 創建的ssl證書
  • 將其綁定到端口8443
  • 已通過“ httpcfg -list”驗證
  • 確保“ 8443.cer”和“ 8443.pkv”位於“ user / .config / .mono / httplistener”中

所以一切應該正常嗎? 假! 這讓我發瘋了幾個小時。

 using System; using System.Net; using System.Text; public class HttpListenerTest { public static void Main(string[] args) { Console.WriteLine("Initialize.."); HttpListener listener = new HttpListener(); listener.Prefixes.Add("http://*:8089/"); listener.Prefixes.Add("https://*:8443/"); listener.Start(); while (true) { Console.WriteLine("Listening.."); HttpListenerContext context = listener.GetContext(); Console.WriteLine("Respond.."); byte[] bytes = Encoding.UTF8.GetBytes("works!"); context.Response.OutputStream.Write(bytes, 0, bytes.Length); context.Response.OutputStream.Flush(); context.Response.OutputStream.Close(); context.Response.Close(); } } } 

如果我運行此代碼並在端口8089上發出http請求,那么一切都很好。 在8443上的https請求終止了該進程,女巫發生的唯一一件事情是在調試模式下發生了“異常事件”( 此處為屏幕截圖 )。

..google搜索丟失的文件名,得到4個無關的結果。

我的知識才剛剛起步,真的需要SSL才能與監聽器一起工作。

因此,如果您對此有任何經驗或建議,請告訴我。

2解決此問題的方法:

如果您需要快速修復本地主機(僅本地主機)

感謝SushiHangovers注釋,將缺少的環境變量'MONO_TLS_PROVIDER'設置為'btls'(BoringSSL)可以解決此問題。

$ MONO_TLS_PROVIDER=btls mono servertest.exe

如果您需要它在服務器上工作

感謝Gusman,這是一篇簡短易懂的指南,介紹了如何使用Nginx來管理SSL並將Http請求發送到Mono服務器。

1.首次安裝/設置Nginx

www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-16-04

2.然后安裝Certbot

certbot.eff.org

3.使用Certbot保護Nginx

www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

4.反向代理到您的HttpListener端口( 從此處獲取

4.1打開Nginx配置文件

 $ sudo nano /etc/nginx/sites-available/default

4.2注釋掉默認的try_files行並指定反向代理設置

 . . .
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;

                # Reverse proxy settings
                include proxy_params;
                proxy_pass http://localhost:8010;
             }
. . . 

並改變

server_name _;

server_name example.com www.example.com;

現在您應該很好了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM