Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 419
Zeus82 Avatar asked Apr 03 '17 21:04

Zeus82


People also ask

How would you enable secret storage in an ASP.NET Core project?

The inner text is arbitrary, but is unique to the project. In Visual Studio, right-click the project in Solution Explorer, and select Manage User Secrets from the context menu.

What is the command for defining a new user secret for an ASP.NET Core project?

All you need to do is select the project in the Solution Explorer window, right-click on the project, and then select Manage User Secrets as shown in Figure 1 below. This will open the secrets. json file in your Visual Studio IDE.

Where is secret json?

Your secrets are stored in a JSON file under your user profile. In a Windows machine, they are stored in the %APPDATA%\Microsoft\UserSecrets\<user_secrets_id>\secrets. json file.


4 Answers

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
like image 147
TeaBaerd Avatar answered Oct 19 '22 20:10

TeaBaerd


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.

like image 38
jakobinn Avatar answered Oct 19 '22 19:10

jakobinn


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>
like image 6
Zeus82 Avatar answered Oct 19 '22 21:10

Zeus82


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.

like image 1
Nagesh Ajab Avatar answered Oct 19 '22 21:10

Nagesh Ajab