Zeus82
Zeus82

Reputation: 6375

ASP Core Cannot Set User Secrets in VS 2017

With visual studio 2017, when I try to set a user secret I get the following error:

> dotnet user-secrets set Authentication:Google:ClientId mysecretclientid
> Could not find the global property 'UserSecretsId' in MSBuild project 'c:\test\myproj.csproj'. Ensure this property is set in the project or use the '--id' command line option.

I have this in my Startup.cs:

[assembly: UserSecretsId("project-8084c8e7-0000-0000-b266-b33f42dd88c0")]

...

builder.AddUserSecrets<Startup>();

If I add this to my csproj:

  <PropertyGroup>
    <UserSecretsId>project-8084c8e7-0000-0000-b266-b33f42dd88c0</UserSecretsId>
  </PropertyGroup>

I get an error saying

Duplicate 'Microsoft.Extensions.Configuration.UserSecrets.UserSecretsIdAttribute'

What am I doing wrong?

Upvotes: 27

Views: 19477

Answers (4)

TeaBaerd
TeaBaerd

Reputation: 1179

In VS Code 2019, you can use the command below to generate the UserSecret section within your csproj file. Please make sure you are within the directory containing the desired csproj file when running the command.

dotnet user-secrets init

Upvotes: 77

jakobinn
jakobinn

Reputation: 2032

I got the same error and I fixed it by generating a new UserSecretsId.

The UserSecretsId will be generated automatically if you right click on the Project in the Solution Explorer and press Manage User Secrets.

Upvotes: 8

Nagesh Ajab
Nagesh Ajab

Reputation: 47

If you are using external caching provider such as redis put this in your .csproj file <PropertyGroup><UserSecretsId>Redistest</UserSecretsId></PropertyGroup>
Redistest is secret name. It could be anything.

Upvotes: 1

Zeus82
Zeus82

Reputation: 6375

Ok, so I got it working, looks like in VS 2017 you should remove the "[assembly: UserSecretsId("project-8084c8e7-0000-0000-b266-b33f42dd88c0")]" attribute and have the following in the csproj file:

<PropertyGroup>
    <UserSecretsId>project-8084c8e7-0000-0000-b266-b33f42dd88c0</UserSecretsId>
</PropertyGroup>

Upvotes: 7

Related Questions