NET Core Kestrel部署HTTPS使用SSL認證

來源:互聯網
上載者:User

標籤:copy   block   ram   版本號碼   line   app   開啟   http   ack   

ASP.NET Core配置 Kestrel部署HTTPS。現在大部分網站已經部署HTTPS,大家對於安全越來越重視。

今天簡單介紹一下ASP.NET Core 部署HTTPS,直接通過配置Kestrel。大家也可以通過前置Nginx來部署HTTPS。

下面直接進入正題。

建立項目並添加引用

建立一個ASP.NET Core Web Application  模板選擇空。

建立好項目添加引用 Microsoft.AspNetCore.Server.Kestrel.Https。

Install-Package Microsoft.AspNetCore.Server.Kestrel.Https

如果你的.NET Core SDK 還是1.0 ,那麼引用的時候加上版本號碼Install-Package Microsoft.AspNetCore.Server.Kestrel.Https -Version 1.0.0

產生認證

通過OpenSSL 產生認證

必須先確保安裝OpenSSL。

首先自己建立根憑證 root 自己做CA也就是發行者。

openssl genrsa -des3 -out root.key

然後按照提示輸入密碼

openssl req -new -key root.key -out root.csr

輸入剛才設定的密碼,然後填寫一些資訊

然後建立一個10年期根憑證 root.crt

openssl x509 -req -days 3650 -sha1 -extensions v3_ca -signkey root.key -in root.csr -out root.crt

建立伺服器憑證


openssl genrsa -des3 -out server.key 2048

openssl req -new -key server.key -out server.req

openssl x509 -req -days 730 -sha1 -extensions v3_req -CA root.crt -CAkey root.key -CAserial root.srl -CAcreateserial -in server.csr -out server.crt


openssl pkcs12 -export -in server.crt -inkey server.key -out server.pfx

 

最終得到的server.pfx 就是可以用來在配置HTTPS。

將server.pfx 複製到項目根目錄

 

添加NuGet包

nuget中尋找然後再程式中添加引用Microsoft.AspNetCore.Server.Kestrel.Https

然後開啟Program.cs 更改代碼如下:

    public class Program    {        public static void Main(string[] args)        {            var host = new WebHostBuilder()                .UseKestrel(option=> {                    option.UseHttps("server.pfx", "linezero");                })                .UseUrls("https://*:443")                .UseContentRoot(Directory.GetCurrentDirectory())                .UseIISIntegration()                .UseStartup<Startup>()                .Build();            host.Run();        }    }

 

然後選擇使用 Kestrel 運行。

開啟瀏覽器輸入 https://localhost/

 

由於認證是自己產生,顯示紅標也就是沒有得到驗證。

附測試用認證下載:server.pfx

NET Core Kestrel部署HTTPS使用SSL認證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.