'How to not get null values from User secret configs?
I want to use user secrets for KeyVault clientid etc. I call my configs from program.cs when I setup KeyVault. When I add the values in appsettings.json all works fine, but if I remove it and manage user secrets it's always say null when my program.cs is running.
And I have the correct values in the created secret.json and the attribute with a guid to it is also in my project file
I have also tried to add a key and value to the secret.json and tried to get it from a controller and it's also null.
So it seems like WebHost.CreateDefaultBuilder in my program.se does not load my secret.json file.
And if I add AssUserSecrets to my WebHost.CreateDefaultBuilder(args) in progam.cs there is no difference same problem.
I'm using WebHost.CreateDefaultBuilder so it shall be used by default in asp .net core 2.2
dontnet user-secret list
Will also shows the values for me from my secret.json file so the value is there, the user-secret has been initialized.
dontnet user-secret init
Also, say it's already setup.
But still just null when calling my secret.json attributes
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureServices(services => services.AddAutofac())
.ConfigureAppConfiguration((context, builder) =>
{
var config = builder.Build();
var vault = config["KeyVault:Vault"];
builder.AddAzureKeyVault($"https://{config["KeyVault:Vault"]}.vault.azure.net/", config["KeyVault:ClientId"], config["KeyVault:ClientSecret"]);
});
Solution 1:[1]
I test in my site and it works fine, here is the steps you could refer to.
1.Right click on the project and select Manage User Secrets. It will open the secrets.json file in Visual Studio where you can add your secrets and it will add the to the .csproj file.
2.Add what you may wish to add to your User Secrets. It includes two ways of writing a json object and a simple key/value pair.

3.Mapping User Secrets to a model.
public class KeyVault
{
public string Vault { get; set; }
public string ClientId { get; set; }
public string ClientSecret { get; set; }
}
4.Adding the Configuration.
Install package Microsoft.VisualStudio.Web.CodeGeneration.Design and Microsoft.Extensions.Configuration and Microsoft.Extensions.Configuration.UserSecrets
5.Bind the values. This is done in the Startup.cs file.

6.Using your mapped secrets
public class HomeController : Controller
{
private readonly KeyVault _keyvault;
// I’ve injected twilioAccountDetails into the constructor
public HomeController(IOptions<KeyVault> keyvault)
{
// We want to know if twilioAccountDetails is null so we throw an exception if it is
_keyvault = keyvault.Value ?? throw new ArgumentException(nameof(keyvault));
}
........
}
For more details about how to use User Secrets in a .NET Core Web App, you could refer to this article.
Solution 2:[2]
I was having this same issue when using Host.CreateDefaultBuilder() and discovered that it was because my host environment was not Development.
To resolve this I added the following section to my launchSettings.json:
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
The following defaults are applied to the returned HostBuilder:
- load app IConfiguration from User Secrets when EnvironmentName is 'Development' using the entry assembly
Solution 3:[3]
In my case, I was missing the following NuGet package:
Microsoft.Extensions.Configuration.UserSecrets
Once I installed this, it worked perfectly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Joey Cai |
| Solution 2 | Peter Csala |
| Solution 3 | Jack Taylor |


