Skip to content

Commit 280b888

Browse files
committed
Update README.md
1 parent b5a6eeb commit 280b888

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,43 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
9191
{
9292
cfg.AddEncryptedAppSettings(crypter =>
9393
{
94-
crypter.CertificatePath = "test-certificate.pfx";
94+
crypter.CertificatePath = "cert.pfx";
9595
crypter.KeysToDecrypt = new List<string> { "Nested:KeyToEncrypt" };
9696
});
9797
});
9898
```
9999

100100
As you can see enabling decryption is as simple as adding a line of code. You just need to specify the path to the certificate and the keys that should be decrypted.
101101

102-
Notice that nested keys have to be separated with colons in this case, because this is the default way to access nested configuration properties in .NET Core ([Link](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1#configuration-keys-and-values)).
102+
If you want to also use the environment specific appsettings files (appsettings.Development.json, appsettings.Production.json) please use the overloaded method to configure as shown below:
103+
104+
```csharp
105+
public static IHostBuilder CreateHostBuilder(string[] args) =>
106+
Host.CreateDefaultBuilder(args)
107+
.ConfigureWebHostDefaults(webBuilder =>
108+
{
109+
webBuilder.UseStartup<Startup>();
110+
})
111+
.ConfigureAppConfiguration((hostingContext, cfg) =>
112+
{
113+
cfg.AddEncryptedAppSettings(hostingContext.HostingEnvironment, crypter =>
114+
{
115+
crypter.CertificatePath = "cert.pfx";
116+
crypter.KeysToDecrypt = new List<string> { "Nested:KeyToEncrypt" };
117+
});
118+
});
119+
```
120+
Notice how to `HostingEnvironment` property of the `hostingContext` variable is used.
121+
122+
Notice that nested keys have to be separated with colons in this case, because this is the default way to access nested configuration properties in .NET Core ([Link](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1#configuration-keys-and-values)).
123+
124+
## How to generate a certificate
125+
Certificates can be generated by using Openssl.
126+
An example certificate is already in the project and the encrypted string in the example appsettings files has been encrypted with it.
127+
To generate a certificate you could use the following commands:
128+
129+
`openssl genrsa 2048 > private.key`
130+
131+
`openssl req -new -x509 -nodes -sha1 -days 365 -key private.key > public.cer`
132+
133+
`openssl pkcs12 -export -in public.cer -inkey private.key -out cert.pfx`

0 commit comments

Comments
 (0)