Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Web App connection strings using ConfigurationBuilder

Tags:

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!

like image 959
Dave New Avatar asked Apr 24 '17 15:04

Dave New


2 Answers

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();
}
like image 157
Amor Avatar answered Sep 24 '22 09:09

Amor


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!

like image 37
David Ebbo Avatar answered Sep 22 '22 09:09

David Ebbo