Could not find the global property 'UserSecretsId' in MSBuild project means
the project file has no <UserSecretsId> element, so
dotnet user-secrets does not know which secret store belongs to it. Run
dotnet user-secrets init once in the project folder, then run your
set command again.
In the test below, init wrote a GUID into the .csproj, and the
same set command that had failed with exit code 1 saved the value on the next
try. dotnet user-secrets list fails with the same message before
init.
The error
dotnet user-secrets set "Jwt:Key" "fix11-test-key-not-a-real-secret-0123456789"
Could not find the global property 'UserSecretsId' in MSBuild project 'C:\...\Sec1\Sec1.csproj'. Ensure this property is set in the project or use the '--id' command line option.
exit=1
The project path is shortened; nothing else in the line changed. The key and the value are made-up strings for the test, not a real secret.
Why it happens
A project made with dotnet new web has no user-secrets id. This is the
project file the template wrote, before anything else was run:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
The tool reads the UserSecretsId property from that file to name the folder
it writes to. With no property and no --id option on the command line, it
stops before writing anything and says so.
The fix
dotnet user-secrets init
Set UserSecretsId to '514b50b0-46e6-4ccb-b589-6a3cf2aeed2e' for MSBuild project 'C:\...\Sec1\Sec1.csproj'.
exit=0
findstr UserSecretsId Sec1.csproj
<UserSecretsId>514b50b0-46e6-4ccb-b589-6a3cf2aeed2e</UserSecretsId>
exit=0
dotnet user-secrets set "Jwt:Key" "fix11-test-key-not-a-real-secret-0123456789"
Successfully saved Jwt:Key to the secret store.
exit=0
dotnet user-secrets list
Jwt:Key = fix11-test-key-not-a-real-secret-0123456789
exit=0
In this project, which has one PropertyGroup, init added the
element as its last line, after ImplicitUsings; commit that
change so everyone on the team uses the same id. The secrets themselves are not in the
project: on this Windows machine the file appeared under
%APPDATA%\Microsoft\UserSecrets\514b50b0-46e6-4ccb-b589-6a3cf2aeed2e\secrets.json,
outside the repository. Your id will be a different GUID. The message also offers
--id, which names a store on the command line instead; init is
the change that makes every later command work without it.
How it was reproduced
A fresh project from dotnet new web -f net10.0 with SDK 11.0.100 on Windows
11. dotnet user-secrets set and list were run
before init, then init, then the same two commands again. The
project file has no package reference before or after init, and
dotnet user-secrets --version printed User Secrets Manager
11.0.0-rtm with no user-secrets tool in dotnet tool list -g, so the command
came with the SDK here.
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 the global property UserSecretsId?
- Run dotnet user-secrets init in the folder that holds the .csproj. It adds a UserSecretsId element with a new GUID, and dotnet user-secrets set and list work from then on.
- Where does dotnet user-secrets store the values on Windows?
- In a secrets.json file under %APPDATA%\Microsoft\UserSecrets, in a folder named after the project's UserSecretsId. In this test that was a folder named with the GUID that init printed, outside the repository.
- Should the UserSecretsId be committed to Git?
- Yes. The id is only the name of the store folder and holds no secret. Committing it lets every developer on the project set their own values under the same id.
More decoded errors in the Fixes category. If the secret is saved but the running app still reads null, see why user-secrets are not loaded outside Development.