You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Reverse Engineering Page is Unclear On Utilizing User Secrets #3939
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.
ID: e1601c3e-3f29-5d84-6350-2b38eeaf6adc
Version Independent ID: 92a856d6-c5da-e215-46ac-d67c411f3ff9
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.
Reacted by Muhab Abdulrahim Almezayyen and Mike A.
Reacted by 11plus10x10, Geneis, Leonardo Rivas, crookseta and Gabriel Majeri
@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":
Activity
Rick-Anderson commentedon Jul 21, 2022
@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:
Program.cs
And it works.
ajcvickers commentedon Jul 23, 2022
@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?
jkoplo commentedon Sep 7, 2022
@Rick-Anderson's code above did not work for me by default (net6) because
CreateDefaultBuilderwill only load secrets if it detects that the environment is "Development", andIHostingEnvironmentisn'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":
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.
bricelam commentedon Sep 7, 2022
I think you can also pass it in via
args:dotnet ef database scaffold ... -- --environment Development