System.InvalidOperationException: Could not find 'UserSecretsIdAttribute' on assembly
'Us2'. came, in this test, from AddUserSecrets<Program>(optional:
false) in a project that was built without a UserSecretsId. The
parameterless AddUserSecrets<Program>() in the same project printed an
empty value and exited 0 instead. Run dotnet user-secrets init in the project
folder, rebuild, and the same code reads the store.
The empty value is the harder case to spot: nothing fails, the setting is just missing.
Us2, the project, and the value below are this test's own names.
The error
dotnet run --no-build
Unhandled exception. System.InvalidOperationException: Could not find 'UserSecretsIdAttribute' on assembly 'Us2'.
Check that the project for 'Us2' has set the 'UserSecretsId' build property.
If the 'UserSecretsId' property is already set then add a reference to the Microsoft.Extensions.Configuration.UserSecrets package.
at Microsoft.Extensions.Configuration.UserSecretsConfigurationExtensions.AddUserSecrets(IConfigurationBuilder configuration, Assembly assembly, Boolean optional, Boolean reloadOnChange)
at Microsoft.Extensions.Configuration.UserSecretsConfigurationExtensions.AddUserSecrets[T](IConfigurationBuilder configuration, Boolean optional)
at Program.<Main>$(String[] args) in C:\...\fixes11\cfg\Us2\Program.cs:line 3
exit=-532462766
The path is shortened; nothing else changed. The top frame is the
AddUserSecrets call itself, so the exception was thrown by that call, before
any value was read. The commands on this page ran in cmd.
Why it happens
The console project had the package but no id:
...
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="10.0.14" />
</ItemGroup>
</Project>
using Microsoft.Extensions.Configuration;
var config = new ConfigurationBuilder()
.AddUserSecrets<Program>(optional: false)
.Build();
Console.WriteLine($"Api:Key = {config["Api:Key"]}");
The project file had no UserSecretsId, and the message says the attribute was
missing from the assembly Us2. The message's third line covers the opposite
case, an id that is set with the package missing; that case was not run here.
The console template set up none of this. The package reference was added with
dotnet add package Microsoft.Extensions.Configuration.UserSecrets --version
10.0.14, and the project file above still had no UserSecretsId after
it. With the parameterless call, the first run of the same project printed:
dotnet run --no-build
Api:Key =
exit=0
The fix
dotnet user-secrets init
Set UserSecretsId to '779de246-2fe9-4963-a2b0-6d37b1486bce' for MSBuild project 'C:\...\fixes11\cfg\Us2\Us2.csproj'.
exit=0
dotnet user-secrets set "Api:Key" "fix11-console-test-value-not-real"
Successfully saved Api:Key to the secret store.
exit=0
dotnet build -v q -nologo
...
dotnet run --no-build
Api:Key = fix11-console-test-value-not-real
exit=0
In this test the run that read the value came after init and a rebuild; a run
of the old build output without rebuilding was not tried. Commit the
UserSecretsId line that init added to the .csproj.
The value is a made-up string for the test.
How it was reproduced
A console project from dotnet new console -f net10.0 with SDK 11.0.100 on
Windows 11 and Microsoft.Extensions.Configuration.UserSecrets 10.0.14. It was run
three times: with the parameterless AddUserSecrets<Program>() (no
exception, empty value), with optional: false (the exception above), and
after init, set and a rebuild (the value printed).
Every command shown without a PS> marker ran in cmd: a PowerShell script passed it as cmd /c ... 2>&1 and printed the exit= line after it.
Frequently asked
- How do I fix Could not find UserSecretsIdAttribute on assembly?
- Run dotnet user-secrets init in the project folder so the .csproj gets a UserSecretsId, then rebuild. In this test the same AddUserSecrets call read the stored value after the rebuild.
- Why does AddUserSecrets return nothing in my console app?
- In this test the parameterless AddUserSecrets<Program>() in a project without a UserSecretsId threw no exception and every value was empty. Passing optional: false turned the same situation into the UserSecretsIdAttribute exception.
- Which package did the console app use for user secrets?
- The test project referenced Microsoft.Extensions.Configuration.UserSecrets 10.0.14, added with dotnet add package and a pinned version. A run without that reference was not made.
More decoded errors in the Fixes category. The command line tool's own version of this problem is Could not find the global property 'UserSecretsId'.