Environment variables

appsettings files

appsettings.json is our base template for the environment variables. Please add your new variables in this file with an empty value. Never write confidential information to this file.

appsettings.Development.json and appsettings.Production.json are the ones you can add your secrets values. These two files are added to .gitignore, so they are not pushed on the git repository.

Use environment variables in the code

public static class AppSettingsHelper
{
    private static IConfiguration _configuration;

    public static void Configure(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public static string GetSetting(string key)
    {
        return _configuration.GetValue<string>(key);
    }
}

In Startup.cs, we automatically configure AppSettingsHelper with the good appsettings file:

AppSettingsHelper.Configure(_configuration);

this static class can be used anywhere in code to get the environment variables (Static classes, services, controllers, etc.) Example of use:

string yourSecret = AppSettingsHelper.GetSetting("Category:SubCategory:YourSecret");

References

results matching ""

    No results matching ""