配置值始终返回null dotnet core 2.1 [英] Configuration value always returned null dotnet core 2.1
问题描述
嗨
我的应用程序是MVC Core 2.1应用程序,我按照此链接中的步骤操作
My app is MVC Core 2.1 application, I followed steps from this link
https://docs.microsoft.com/zh-cn/aspnet/core/security/key-vault-configuration?view=aspnetcore-2.1
https://docs.microsoft.com/en-us/aspnet/core/security/key-vault-configuration?view=aspnetcore-2.1
首先在Azure中,我已经注册了我的应用程序,并获取了我的三个参数KeyVault,appID和appSecret,并将它们放在一个名为azurekeyvault.json的json文件中,这里是其内容.
First in azure I've registred my application and get my three parameters the KeyVault, the appID and the appSecret and I put them in a json file that I named azurekeyvault.json here is its content
{
"azureKeyVault":{
"保险柜":"keyvault-XXXX",
"ClientID":"XXX-XXX-XXX-XXX-XXX",
" ClientSecret" ;:" YYYY"
}
}
{
"azureKeyVault": {
"Vault": "keyvault-XXXX",
"ClientID": "XXX-XXX-XXX-XXX-XXX",
"ClientSecret": "YYYY"
}
}
此后,我在startup.cs中添加了一个私有方法,该方法是从configure方法调用的
After that I added a private method in startup.cs which I called from configure method
private void configAzureInit(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("azurekeyvault.json",可选:false);
var config = builder.Build();
builder.AddAzureKeyVault($"https://{config [" azureKeyVault:Vault]}.vault.azure.net/",
config ["azureKeyVault:ClientID"],
config ["azureKeyVault:ClientSecret"]);
builder.Build();
private void configAzureInit(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("azurekeyvault.json", optional: false);
var config = builder.Build();
builder.AddAzureKeyVault($"https://{config["azureKeyVault:Vault"]}.vault.azure.net/",
config["azureKeyVault:ClientID"],
config["azureKeyVault:ClientSecret"]);
builder.Build();
}
}
如果我使用
测试参数的检索,
var conn = config ["appSettings:connectionStings:parametername"];
if I test the retrival of the parameter with
var conn = config["appSettings:connectionStings:parametername"];
没有编译错误,总是获得空值,问题是代码有什么问题,我错过了一些步骤
There is not compilation errors and always get null value, the question is that somthing wrong with the code and did I miss some steps
谢谢
复杂性在于简单性跟我来:http://smartssolutions.blogspot.com
The complexity resides in the simplicity Follow me at: http://smartssolutions.blogspot.com
推荐答案
您是否尝试过包括这些参数在应用程序设置(而不是JSON文件)中进行引用,就像在 这个Github示例?
Have you tried including these parameters in the app settings instead of the JSON file and referencing that, as done in this Github sample?
该示例指南非常有用:https://github.com/aspnet/Docs/tree/master/aspnetcore/security/key-vault-configuration/samples/basic-sample/2.x
This sample guide is pretty helpful: https://github.com/aspnet/Docs/tree/master/aspnetcore/security/key-vault-configuration/samples/basic-sample/2.x
这篇关于配置值始终返回null dotnet core 2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!