1

Use .NET Secrets in a Console Application

 2 years ago
source link: https://www.programmingwithwolfgang.com/use-net-secrets-in-console-application/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Use .NET Secrets in a Console Application

Jan 232022-01-24T00:00:00+01:00 by Wolfgang Ofner

.NET Core made it easy to configure your application. Currently, I am working on a .NET 6 console application and this showed me that especially ASP.NET MVC makes it easy to set up middleware such as dependency injection. When using a console application, it is not hard but it requires a bit more work than the web application.

In this post, I would like to show you how to use .NET secrets in your .NET 6 console application.

Create a new .NET 6 console application

You can find the code of the demo on GitHub.

Create a new .NET 6 console application using your favorite IDE or the command line. First, add install the following two NuGet packages

Install-Package Microsoft.Extensions.Configuration Install-Package Microsoft.Extensions.Hosting

Next, create a new class, that will read the appsettings file and also the NETCORE_ENVIRONMENT environment variable.

using Microsoft.Extensions.Configuration;

namespace ReadSecretsConsole;

public class SecretAppsettingReader { public T ReadSection<T>(string sectionName) { var environment = Environment.GetEnvironmentVariable("NETCORE_ENVIRONMENT"); var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{environment}.json", optional: true) .AddEnvironmentVariables(); var configurationRoot = builder.Build();

return configurationRoot.GetSection(sectionName).Get<T>(); } }

The NETCORE_ENVIRONMENT variable is the default variable to configure your environment in .NET. This variable contains values such as Development or Production and can be used to read a second appsettings file for the specific environment. For example, in production, you have a file called appsettings.Production.json which overrides some values from the appsettings.json file.

Next, add a new file, called appsettings.json, and add the following code there:

{ "MySecretValues": { "Username": "Abc", "Password": "Xyz" } }

This file contains a username and password. Values that should never be checked into source control!

Lastly, add the following code to your Program.cs file:

using ReadSecretsConsole;

var secretAppsettingReader = new SecretAppsettingReader(); var secretValues = secretAppsettingReader.ReadSection<SecretValues>("MySecretValues");

Console.WriteLine($"The user name is: {secretValues.Username}, and the password is: {secretValues.Password}");

Console.ReadKey();

This code creates a new instance of SecretAppsettingReader and then reads the values from the appsettings.json file. Start the applications and you should see the values printed to the console.

Read the values from appsettings

Add Secrets to your Application

The application works and reads the username and password from the appsettings file. If a developer adds his password during the development, it is possible that this password gets forgotten and ends up in the source control. To mitigate accidentally adding passwords to the source control, .NET introduced secrets.

To add a secret, right-click on your project and then select “Manage User Secrets” in Visual Studio.

Add a User Secret

This should create a secrets.json file and add the Microsoft.Extensions.Configuration.UserSecrets NuGet package. Sometimes Visual Studio 2022 doesn’t install the package, so you have to install it by hand with the following command:

Install-Package Microsoft.Extensions.Configuration.UserSecrets

You can use the secrets.json file the same way as you would use the appsettings.json file. For example, add the “MySecretValues” section and a new value for the “Password”:

{ "MySecretValues": { "Password": "SecretPassword" } }

There is one more thing you have to do before you can use the secrets.json file. You have to read the file using AddUserSecrets in the SecretAppsettingReader file:

using Microsoft.Extensions.Configuration;

namespace ReadSecretsConsole;

public class SecretAppsettingReader { public T ReadSection<T>(string sectionName) { var environment = Environment.GetEnvironmentVariable("NETCORE_ENVIRONMENT"); var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{environment}.json", optional: true) .AddUserSecrets<Program>() .AddEnvironmentVariables(); var configurationRoot = builder.Build();

return configurationRoot.GetSection(sectionName).Get<T>(); } }

The AddUserSecrets method takes a type that indicates in what assembly the secret resides. Here I used Program, but you could use any other class inside the assembly.

Start the application and you should see that the password is the same value as in the secrets.json file.

The password was read from the secret

When you check in your code into Git, you will see that there is no secrets.json file to be checked in. Therefore, it is impossible to check in your secrets like passwords.

Conclusion

Secrets help developers to keep their repositories free of passwords and other sensitive information like access tokens. .NET 6 allows you to set up these secrets with only a couple of lines of code.

You can find the code of the demo on GitHub.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK