I follow this tutorial: https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-3.1&tabs=windows#access-a-secret
To a .NET Core 3.1 project I've added Microsoft.Extensions.Configuration.UserSecrets
package, I have clicked on the project to manage secrets, the file has been created in the AppData
directory and the UserSecretsId
has been added automatically in .csproj
file.
Because the Host.CreateDefaultBuilder
didn't load secrets I decided to add them manually
if (hostContext.HostingEnvironment.IsDevelopment())
{
builder.AddUserSecrets(Assembly.GetExecutingAssembly());
}
})
.Build();
But then I get
System.InvalidOperationException
HResult=0x80131509
Message=Could not find 'UserSecretsIdAttribute' on assembly 'XXX'.
Check that the project for 'XXX' has set the 'UserSecretsId' build property.
If the 'UserSecretsId' property is already set then add a reference to the Microsoft.Extensions.Configuration.UserSecrets package.
Source=Microsoft.Extensions.Configuration.UserSecrets
I've checked the suggestion in the expecion's description and both predicaments are fulfilled.
You should have the UserSecretsId listed in your .csproj file. If you right click on the API layer and manage user secrets confirm that the GUID matches in the AppData directory with whats in the project.
Your Program.cs class should be using the CreateDefaultBuilder, you should see that there is a section to include Secrets when the environment is Development. Check your ASP
"ASPNETCORE_ENVIRONMENT": "Development"
is set to development. Lastly I'm assuming your executing assembly has the PropertyGroup for the user secrets mentioned above.