How to get "Manage User Secrets" in a .NET Core console-application?

.net-core, asp.net-core-1.0, visual-studio-2017

Solution

"Manage user secrets" from a right click is only available in web projects.

There is a slightly different process for console applications

It requires manually typing the required elements into your csproj file then adding secrets through the PMC

I have outlined the process that worked for me in my current project step by step in this blog post :

https://medium.com/@granthair5/how-to-add-and-use-user-secrets-to-a-net-core-console-app-a0f169a8713f

tl;dr

Step 1

Right click project and hit edit projectName.csproj

Step 2

add `<UserSecretsId>Insert New Guid Here</UserSecretsId>` into csproj under TargetFramework

add `<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0"/>` within Item Group in csproj

Step 3

Open PowerShell (admin) cd into project directory and

enter `dotnet user-secrets set YourSecretName "YourSecretContent"`

This will create a secrets.json file in:

%APPDATA%\microsoft\UserSecrets\<userSecretsId>\secrets.json

Where userSecretsId = the new Guid you created for your csproj

Step 4

Open secrets.json and edit to look similar to this

{
 "YourClassName":{
    "Secret1":"Secret1 Content",
    "Secret2":"Secret2 Content"
   }
} 

By adding the name of your class you can then bind your secrets to an object to be used.

Create a basic POCO with the same name that you just used in your JSON.

namespace YourNamespace
{
    public class YourClassName
    {
        public string Secret1 { get; set; }
        public string Secret2 { get; set; }
    }
}

Step 5

Add `Microsoft.Extensions.Configuration.UserSecrets` Nuget package to project

Add

var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddUserSecrets<YourClassName>()
.AddEnvironmentVariables();

&

var services = new ServiceCollection()
.Configure<YourClassName>(Configuration.GetSection(nameof(YourClassName)))
.AddOptions()
.BuildServiceProvider();

services.GetService<SecretConsumer>();

To your Program.cs file.

Then inject `IOptions<YourClassName>` into the constructor of your class

private readonly YourClassName _secrets;

public SecretConsumer(IOptions<YourClassName> secrets)
{
  _secrets = secrets.Value;
}

Then access secrets by using `_secrets.Secret1;`

Thanks to Patric for pointing out that `services.GetService<NameOfClass>();` should be `services.GetService<SecretConsumer>();`

Problem

When I create a new ASP .NET Core Web-Application, I can right-click the project in Visual Studio, and I see a context-menu entry called "Manage User Secrets". When I create a new .NET Core Console-Application, I don't see this context-menu entry. However, a "Web"-Application shows as "console" application in the project settings. Is there any way I can get this context-menu entry in a console-application ?

Original source