Skip to content

Reverse Engineering Page is Unclear On Utilizing User Secrets #3939

Closed

Description

@Tohron

The page is very unclear as to how one should actually store user secrets in a way so that
dotnet ef dbcontext scaffold Name=ConnectionStrings:Chinook Microsoft.EntityFrameworkCore.SqlServer
could access them. Does the command automatically know where to look for the secrets. Is there a specific format in the secrets file to indicate a field is the database password that should be used? The page doesn't say.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Activity

  1. Rick-Anderson commented on Jul 21, 2022

    @Rick-Anderson
    Contributor

    @Tohron We need to update Safe storage of app secrets in development in ASP.NET Core to at least point to an updated version of Configuration in .NET. Using the two documents, the following works in a console app:

    dotnet user-secrets init
    dotnet user-secrets set KeyOne 1
    dotnet user-secrets set KeyTwo true
    

    Program.cs

    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    
    using IHost host = Host.CreateDefaultBuilder(args).Build();
    
    // Ask the service provider for the configuration abstraction.
    IConfiguration config = host.Services.GetRequiredService<IConfiguration>();
    
    // Get values from the config given their key and their target type.
    int keyOneValue = config.GetValue<int>("KeyOne");
    bool keyTwoValue = config.GetValue<bool>("KeyTwo");
    
    
    // Write the values to the console.
    Console.WriteLine($"KeyOne = {keyOneValue}");
    Console.WriteLine($"KeyTwo = {keyTwoValue}");
    
    
    // Application code which might rely on the config could start here.
    
    await host.RunAsync();
    

    And it works.

  2. ajcvickers commented on Jul 23, 2022

    @ajcvickers
    Contributor

    @Tohron From an EF perspective, we link to the User Secrets docs for this. Do you think something is missing from the EF side here?

  3. jkoplo commented on Sep 7, 2022

    @jkoplo

    @Rick-Anderson's code above did not work for me by default (net6) because CreateDefaultBuilder will only load secrets if it detects that the environment is "Development", and IHostingEnvironment isn't set properly in a console app.

    There are a ton of possible workarounds, but for my purposes it was throwaway test code so I simply forced it to "Development":

    using IHost host = Host.CreateDefaultBuilder(args).UseEnvironment("Development").Build();

    Currently this GH issue is linked from the Microsoft docs (https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-6.0&tabs=windows) so I think others may find their way here.

  4. bricelam commented on Sep 7, 2022

    @bricelam
    Contributor

    I think you can also pass it in via args: dotnet ef database scaffold ... -- --environment Development

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions