5

I am having a problem when trying to read user secrets. My code in startup.cs is as follows:

 public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json",
                    optional: false,
                    reloadOnChange: true);

            if (env.IsDevelopment())
            {
                builder.AddUserSecrets<Startup>(false);
            }

            builder.AddEnvironmentVariables();
            Configuration = builder.Build();
        }

Reading from appsettings.json and from environment variables works fine, but no matter what I put in the secrets.json file no values are read from it. The value of UserSecretsId in the project file is correct and env.IsDevelopment() is true.

Configuration contains three providers. One JsonConfigurationProvider containing the Data from appsettings.json, one EnvironmentVariablesConfigurationProvider containing the values from environment variables and one JsonConfigurationProvider which has no data. I presume the latter one is the one from adding user secrets. But why is it empty?

Matt
  • 25,467
  • 18
  • 120
  • 187
RHG
  • 51
  • 3

3 Answers3

1

I had to split a project into two parts but I could not for the life of me get the secrets to load in the spawned project, it had exactly the same project environment, including the secrets.json in the VS2017 project tree.... but it would just not load.

On inspection of the actual .csproj files on each, the only difference was that the spawned project did not have the the UserSecretsId entry so I just right-clicked on the project, hit Manage User Secrets and then closed the ensuing Json editor, without modification.

When I ran the project next, my original secrets.json was loaded... not the blank one.

It appears that if you have secrets.json within your project folder, this over-rides the file created by the Manage User Secrets which creates them in C:\Users\<user>\AppData\Roaming\Microsoft\UserSecrets\<userSercretsId>

Chris Hammond
  • 2,064
  • 5
  • 27
  • 53
0

I got it working like this in a test project. In the Startup.cs, I did:

    internal IConfigurationRoot Configuration { get; private set;  }

    public override void ConfigureTestServices(ref IServiceCollection services)
    {        
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json",
                optional: false,
                reloadOnChange: true);
        builder.AddUserSecrets<Startup>(false);
        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
        services.AddScoped<IConfigurationRoot>(option => Configuration);
    

      // ... more settings
    }
  1. Inside the configuration file, you can simply access it via

         var secretValue = Configuration["SomeUserSecret"];
    
  2. Outside, in your test classes, you're using dependency injection by specifying IConfigurationRoot config in your constructor(s) and assign config to a class property and then access it like

         var secretValue = config["SomeUserSecret"];
    

Regarding the preparation, I found this useful hint describing a couple of preparation steps for your project. In a nutshell, you need to edit the csproj file and

  • add <UserSecretsId>Insert New Guid Here</UserSecretsId> under TargetFramework
  • add <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0"/> within Item Groups
  • reload the csproj file - then you can right click on it and select "Manage User Secrets", which will create the secrets.json file for you.
Matt
  • 25,467
  • 18
  • 120
  • 187
-1

try without false. e.g builder.AddUserSecrets();

I had the same problem.