Skip to content

Commit c83906b

Browse files
authored
dotnet: improve dev-certs instructions
1 parent 88d2d24 commit c83906b

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

src/dotnet/README.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,62 @@ See [history](history) for information on the contents of each version and [here
3939

4040
Alternatively, you can use the contents of [.devcontainer](.devcontainer) to fully customize your container's contents or to build it for a container host architecture not supported by the image.
4141

42-
### Enabling HTTPS in ASP.NET using your own dev certificate
42+
### Enabling HTTPS in ASP.NET Core
4343

44-
To enable HTTPS in ASP.NET, you can mount an exported copy of your local dev certificate.
44+
You can use `dotnet dev-certs https` inside the Dev Container to create a development HTTPS certificate for ASP.NET Core. However, each time the container is recreated, the development certificate will be lost. To make the development certificate survive container rebuilds, you can use a named volume.
4545

46-
1. Export it using the following command:
46+
For example, in `devcontainer.json`, add a named volume for the `x509stores` directory inside the `vscode` user's home folder. Also add a lifecycle script, which adds the development certificate to the Dev Container's trust store.
47+
48+
``` json
49+
"mounts": [
50+
{
51+
"type": "volume",
52+
"source": "x509stores",
53+
"target": "/home/vscode/.dotnet/corefx/cryptography/x509stores"
54+
}
55+
],
56+
"onCreateCommand": "bash .devcontainer/on-create.sh"
57+
```
58+
59+
The contents of `.devcontainer/on-create.sh`:
60+
61+
``` bash
62+
#!/usr/bin/env bash
63+
64+
# Change ownership of the .dotnet directory to the vscode user (to avoid permission errors)
65+
sudo chown -R vscode:vscode /home/vscode/.dotnet
66+
67+
# Export the ASP.NET Core HTTPS development certificate to a PEM file
68+
# If there is no development certificate, this command will generate a new one
69+
DOTNET_NOLOGO=true \
70+
DOTNET_GENERATE_ASPNET_CERTIFICATE=false \
71+
dotnet dev-certs https --export-path /home/vscode/https.crt --format pem
72+
73+
# Add the PEM file to the trust store
74+
sudo mv /home/vscode/https.crt /usr/local/share/ca-certificates/https.crt
75+
sudo update-ca-certificates
76+
```
77+
78+
You should see the following output when the Dev Container is created:
79+
80+
``` text
81+
Running the onCreateCommand from devcontainer.json...
82+
83+
The HTTPS developer certificate was generated successfully.
84+
Updating certificates in /etc/ssl/certs...
85+
rehash: warning: skipping ca-certificates.crt,it does not contain exactly one certificate or CRL
86+
1 added, 0 removed; done.
87+
Running hooks in /etc/ca-certificates/update.d...
88+
done.
89+
```
90+
91+
Now this certificate will survive container rebuilds. The certificate will also be trusted by code running inside the container like `System.Net.HttpClient`, or tools like wget and curl. If needed, you can use Docker Desktop to export the development certificate to a local directory, in case you need to add it to any other trust stores.
92+
93+
#### Alternate solution
94+
95+
You can mount an exported copy of your local dev certificate. This solution is only suitable for private projects, as the password will become part of your `devcontainer.json`. Do not apply this solution to team projects or open source projects.
96+
97+
1. Export the local certificate using the following command:
4798

4899
**Windows PowerShell**
49100

0 commit comments

Comments
 (0)