Cannot load user secrets in ASP.NET Core 2.2

In ASP.NET Core 2.2, there is an issue which may block you from loading user secrets:
https://github.com/aspnet/Extensions/issues/795

Basically, since ASP.NET Core 2.0, when you calls CreateDefaultBuilder to initialize a new instance of the host, the user secrets configuration source is automatically added in development mode.

1
2
3
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>();

However, in ASP.NET Core 2.2, you may find that your user secrets won’t work. The solution is to install directly the package Microsoft.Extensions.Configuration.UserSecrets into the main project.

1
Install-Package Microsoft.Extensions.Configuration.UserSecrets

Behind the scene, ASP.NET Core 2.2 finds UserSecretsIdAttribute in the main program assembly. This attribute is auto-generated from property UserSecretsId in .csproj file during MSBuild via this target here. In version 2.2, this target is only executed when the package Microsoft.Extensions.Configuration.UserSecrets is directly referenced in the main project.

Leave a Reply