Secret Manager Tool

From Logic Wiki
Jump to: navigation, search


Installation

Add Microsoft.Extensions.SecretManager.Tools to the .csproj file and run dotnet restore.

in csproj file :

 <ItemGroup>
   <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0-msbuild3-final" />
 </ItemGroup>

Test the Secret Manager tool by running the following command:

dotnet user-secrets -h

Usage

The Secret Manager tool operates on project-specific configuration settings that are stored in your user profile. To use user secrets, the project must specify a UserSecretsId value in its .csproj file. The value of UserSecretsId is arbitrary, but is generally unique to the project. Developers typically generate a GUID for the UserSecretsId.

Add a UserSecretsId for your project in the .csproj file:

<PropertyGroup>
  <UserSecretsId>My-USER-SECRET-ID-HERE-c23d27a4-eb88</UserSecretsId>
</PropertyGroup>


dotnet user-secrets set MySecret ValueOfMySecret

You can run the Secret Manager tool from other directories, but you must use the --project option to pass in the path to the .csproj file:

dotnet user-secrets set MySecret ValueOfMySecret --project c:\work\WebApp1\src\webapp1

Accessing user secrets via configuration

You access Secret Manager secrets through the configuration system. Add the Microsoft.Extensions.Configuration.UserSecrets package and run dotnet restore. Add the user secrets configuration source to the Startup method:

public Startup(IHostingEnvironment env)
       {
           var builder = new ConfigurationBuilder();
           if (env.IsDevelopment())
           {
               builder.AddUserSecrets<Startup>();
           }
           Configuration = builder.Build();
       }

You can access user secrets via the configuration API:

public void ConfigureServices(IServiceCollection services)
       {
           _testSecret = Configuration["MySecret"];
       }

How the Secret Manager tool works

The Secret Manager tool abstracts away the implementation details, such as where and how the values are stored. You can use the tool without knowing these implementation details. In the current version, the values are stored in a JSON configuration file in the user profile directory:

  • Windows: %APPDATA%\microsoft\UserSecrets\<userSecretsId>\secrets.json
  • Linux: ~/.microsoft/usersecrets/<userSecretsId>/secrets.json
  • Mac: ~/.microsoft/usersecrets/<userSecretsId>/secrets.json

The value of userSecretsId comes from the value specified in .csproj file.


You should not write code that depends on the location or format of the data saved with the Secret Manager tool, as these implementation details might change. For example, the secret values are currently not encrypted today, but could be someday.