我们将一些敏感密钥和连接字符串存储在 Web App 应用程序设置下的连接字符串部分:
我们使用以下方法检索配置设置ConfigurationBuilder
:
Configuration = new ConfigurationBuilder()
.SetBasePath(environment.ContentRootPath)
.AddEnvironmentVariables()
.Build();
我本来希望AddEnvironmentVariables()
拿起这些连接字符串,但事实并非如此。请注意,如果您在 Web 应用程序中将这些值设置为“应用程序设置”,这确实有效。
经过仔细检查(使用 Kudu 控制台),我发现为这些连接字符串设置的环境变量具有CUSTOMCONNSTR_前缀到键名:
CUSTOMCONNSTR_MongoDb:ConnectionString=...
CUSTOMCONNSTR_Logging:ConnectionString=...
CUSTOMCONNSTR_ApplicationInsights:ChronosInstrumentationKey=...
我现在应该如何使用ConfigurationBuilder
?
编辑:
我发现一个方便的AddEnvironmentVariables
重载存在一个prefix
参数,描述为:
// prefix:
// The prefix that environment variable names must start with. The prefix will be
// removed from the environment variable names.
但是添加.AddEnvironmentVariables("CUSTOMCONNSTR_")
到配置生成器也不起作用!