--- lang: ja-jp breaks: true --- # ASP.NET Core Kestrel へのSSL証明書の組み込み 2023-12-02 > Kestrel Web サーバーにSSL証明書をバインドする - Kestrel Web サーバーへのSSL証明書の組み込み (ASP.NET プログラミング) > https://www.ipentec.com/document/csharp-asp-net-core-bind-ssl-certification-in-kestrel-web-server ## HTTPS SSL証明書を作成 > XCA(X Certificate and Key management) による HTTPS SSL証明書の作成 2021-09-22 > https://hackmd.io/GZQBOIP_QnmP832Ly5efsw ![image](https://hackmd.io/_uploads/Bkf2cLtST.png) ### ルート証明書をエクスポートします。 フォーマットは、「PEM(*.crt)」を選択します。 ![image](https://hackmd.io/_uploads/Sk5Ah8KSp.png) ### ルート証明書を「信頼されたルート証明機関」にインストールします。 ![image](https://hackmd.io/_uploads/ryZdp8YST.png) ### サーバ証明書をエクスポートします。 * エクスポートフォーマットで「PKCS #12 chain(*.pfx)」を選択します。 ![image](https://hackmd.io/_uploads/S1MBjIFH6.png) ## ASP.NET Core のプロジェクトを作成 ![image](https://hackmd.io/_uploads/BkQE0UKB6.png) ![image](https://hackmd.io/_uploads/SJTLCUFS6.png) ### プロジェクトにサーバ証明書を追加して出力ディレクトリにコピーする設定を行います ![image](https://hackmd.io/_uploads/Bynh0UtBp.png) ![image](https://hackmd.io/_uploads/Bybe1DtHa.png) ### Kestrel の設定を行います。 * appsettings.json に `"Kestrel"` の部分を丸々追加します。 ```json= { "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "Kestrel": { "Endpoints": { "Http": { "Url": "http://*:80" }, "Https": { "Url": "https://*:443", "ClientCertificateMode": "AllowCertificate", "Certificate": { "Path": "localhost_https_server.pfx", "Password": "" } } } }, "AllowedHosts": "*" } ``` ### launchSettings.json に `"externalUrlConfiguration": true` を追加。 > Removing Kestrel binding warning > https://stackoverflow.com/questions/51738893/removing-kestrel-binding-warning * launchSettings.json ```json= { "$schema": "http://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:46389", "sslPort": 44305 } }, "profiles": { "http": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "http://localhost:5195", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "https": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "https://localhost:7216;http://localhost:5195", "externalUrlConfiguration": true, //add this line "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } } ``` ## プロジェクトを発行 ![image](https://hackmd.io/_uploads/HJsvrPKBp.png) ![image](https://hackmd.io/_uploads/SkVtrDFS6.png) ![image](https://hackmd.io/_uploads/S1ZqrPFSa.png) ![image](https://hackmd.io/_uploads/HkVorPtrT.png) ![image](https://hackmd.io/_uploads/B1p6BDYrp.png) ## 実行確認 ![image](https://hackmd.io/_uploads/H1Ig8PYr6.png) ![image](https://hackmd.io/_uploads/SyHzIPFHT.png) ![image](https://hackmd.io/_uploads/ryfB8wKS6.png) ###### tags: `ASP.NET Core` `Kestrel` `SSL証明書` `https`