【发布时间】:2018-05-07 09:54:06
【问题描述】:
我有一个 .net core 2.0 应用程序,有多个 appsettings 文件。我有 appsettings.json、appsettings.Development.json 和自定义 appsettings.Secure.json。
//appSettings.json
{
"AzureAd" : {
"ClientId" : "CID"
}
}
//appSettings.Development.json
{
"AzureAd" : {
"Random" : "random2"
}
}
//appSettings.Secure.json
{
"AzureAd" : {
"ClientSecret" : "CSECRET"
}
}
我总是希望加载我的 appSettings.Secure.json 配置。以下是 json 文件的配置方式。
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appSettings.json", optional : false)
.AddJsonFile("appSettings.Development.json")
.AddJsonFile("appsettings.Secure.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
问题是我的“AzureAd:ClientSecret”被加载到配置中(在配置对象的 Providers 部分中列出)但是当我注入 IOption 时,ClientSecret 属性为空。
AzureAdOptions是属性映射到的类,注册方式如下:
services.Configure<AzureAdOptions>(Configuration.GetSection("AzureAd"));
【问题讨论】:
-
不直接相关,但是:
I always want my appSettings.Secure.json configs to be loaded- 那你为什么将它指定为optional: true?根据需要标记它 => 至少当此文件不可读取时,您总是会遇到错误 -
请显示
AzureAdOptions类的定义 -
@Set public class AzureAdOptions { public string ClientId { get;放; } 公共字符串 ClientSecret { 获取;放; } 公共字符串实例 { 获取;放; } 公共字符串域 { 获取;放; } 公共字符串 TenantId { 获取;放; } 公共字符串 RedirectUrl { 获取;放; } 公共列表 权限 { 获取;放; } }
标签: asp.net .net asp.net-core asp.net-core-2.0