如何在本機端的開發環境建立 .net core SSL 開發憑證

Posted by Ryan Tseng on 2019-06-09

環境

  • macOS Mojave 10.14.5
  • Rider 2019.1.2

前情提要

今天會提到這篇,主要是因為之前有一次使用 Rider 建立新的專案時,都會跑出錯誤,雖然不影響開發,但就是覺得有點煩,所以就乾脆研究一下怎樣產生這個 ssl 本機開發憑證。

步驟

首先建立一個新的 API 專案,然後這邊的 .net core 版本是 2.2.107

Rider 其實蠻用心的,建立完專案會在右下角跑出這個提醒,告訴你還沒建立 developer 憑證,並且給你一個按鈕看要不要直接設定,不過今天這篇文章不走這個方式進行設定。

這時候什麼都不做,直接執行起來就會看到下面的錯誤訊息,而且基本上錯誤訊息已經告訴你要怎麼做了

1
2
3
4
5
6
7
// 略
crit: Microsoft.AspNetCore.Server.Kestrel[0]
Unable to start Kestrel.
System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.
// 略

錯誤訊息裡面告訴我們,要產生 developer certificate 就執行 dotnet dev-certs https 指令,而如果要 trust 這個憑證後面再加上一個 --trust 參數即可,並且這是 Windows, macOS 才能使用的指令

事不宜遲直接來試試看,結果發現沒有加上 --trust 的指令,執行之後還是會有警告

那們我們就加上 --trust 看看有什麼變化

這個時候就會發現,我們已經能夠直接在本機端透過 dev-certs 製作的 ssl 憑證進行訪問

補充資料