5

We are storing some of our sensitive keys and connection strings in the Connection strings section under the Web App application settings:

Connection strings Azure Web App

We retrieve configuration settings using the ConfigurationBuilder:

Configuration = new ConfigurationBuilder()
    .SetBasePath(environment.ContentRootPath)
    .AddEnvironmentVariables()
    .Build();

I would have expected AddEnvironmentVariables() to pick up these connection strings, but it doesn't. Note that this does work if you set these values as "App settings" in the Web App.

Under closer inspection (using the Kudu console) I found that the environment variables being set for these connections strings have CUSTOMCONNSTR_ prefixed to the key name:

CUSTOMCONNSTR_MongoDb:ConnectionString=...
CUSTOMCONNSTR_Logging:ConnectionString=...
CUSTOMCONNSTR_ApplicationInsights:ChronosInstrumentationKey=...

How should I now read in these connection strings using the ConfigurationBuilder?

EDIT:

I found that a handy AddEnvironmentVariables overload exists with a prefix parameter, described as:

//   prefix:
//     The prefix that environment variable names must start with. The prefix will be
//     removed from the environment variable names.

But adding .AddEnvironmentVariables("CUSTOMCONNSTR_") to the configuration builder doesn't work either!

2 Answers 2

2

But adding .AddEnvironmentVariables("CUSTOMCONNSTR_") to the configuration builder doesn't work either!

AddEnvironmentVariables with prefix just add a limit for environment variables which must with the specified prefix. It will not change the environment variables.

To retrieve value from connection string configuration, you could use code as following.

Configuration.GetConnectionString("MongoDb:ConnectionString");

For hierarchical structure setting, please add it to app settings instead of connection strings on Azure portal.

How should I now read in these connection strings using the ConfigurationBuilder?

As a workaround, you could re-add EnvironmentVariable and rebuild the ConfigurationBuilder after you get the connection string. Code below is for your reference.

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();

    Configuration = builder.Build();
    //Add EnvironmentVariable and rebuild ConfigurationBuilder
    Environment.SetEnvironmentVariable("MongoDb:ConnectionString", Configuration.GetConnectionString("MongoDb:ConnectionString"));
    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
}
Sign up to request clarification or add additional context in comments.

Comments

2

It should just work, and it does for me in my sample app: https://github.com/davidebbo-test/AspNetCoreDemo. Specifically:

  • MyDatabase connection string is defined here.
  • It is used here.
  • If you define MyDatabase conn string in Azure Portal, you will see the new value at runtime (go to the About page).

So start by verifying that mine works, and try to see what you may be doing differently. You should never need to make any assumptions on the CUSTOMCONNSTR_ prefix!

2 Comments

I am not storing these in the JSON file - only in the Web App settings (thus needing to retrieve them from the environment variables). I've removed AddJsonFile() from the configuration above for clarity. Where is this About page? I can indeed see the connection string in the env vars, but only with the prefixed key.
It works in my app even without setting it in the JSON (i.e. only the Azure Connection String). There is an About link at the top when you run it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.