---
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

### ルート証明書をエクスポートします。
フォーマットは、「PEM(*.crt)」を選択します。

### ルート証明書を「信頼されたルート証明機関」にインストールします。

### サーバ証明書をエクスポートします。
* エクスポートフォーマットで「PKCS #12 chain(*.pfx)」を選択します。

## ASP.NET Core のプロジェクトを作成


### プロジェクトにサーバ証明書を追加して出力ディレクトリにコピーする設定を行います


### 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"
}
}
}
}
```
## プロジェクトを発行





## 実行確認



###### tags: `ASP.NET Core` `Kestrel` `SSL証明書` `https`