AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Startup.cs not fully compatible with .NET 6 secrets

Open fceci90 opened this issue 3 years ago • 2 comments

I have an issue with the following statement: "Apps migrating to 6.0 don't need to use the new minimal hosting model."

It seems as though secrets.json will not get read if you are not using the new minimal hosting model, and decide to use Startup.cs. We upgraded an app from .NET Framework 4.7.2 and the upgrade assistant upgraded to .NET 6 using Startup.cs insted of the minimal hosting model. However, configuration won't read from secrets.json.

Based on this documentation here https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-6.0&tabs=windows:

WebApplication.CreateBuilder initializes a new instance of the WebApplicationBuilder class with preconfigured defaults. The initialized WebApplicationBuilder (builder) provides default configuration and calls AddUserSecrets when the EnvironmentName is Development

I don't see any way to call AddUserSecrets from within Startup.cs or Program.cs when using .NET 6 with Startup.cs.

Switching to the .NET 5 docs here https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-5.0&tabs=windows I see the following:

The user secrets configuration provider registers the appropriate configuration source with the .NET Configuration API.

The user secrets configuration source is automatically added in Development mode when the project calls CreateDefaultBuilder. CreateDefaultBuilder calls AddUserSecrets when the EnvironmentName is Development:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

This is the code added when using the upgrade assistant.

When replacing CreateHostBuilder(args).Build().Run(); in Program.cs Main method with code that explicitly adds secrets.json as per the .NET 5 docs, the solution won't run in .NET 6:

public class Program
{
    public static void Main(string[] args)
    {
        var host = new HostBuilder()
            .ConfigureAppConfiguration((hostContext, builder) =>
            {
                // Add other providers for JSON, etc.

                if (hostContext.HostingEnvironment.IsDevelopment())
                {
                    builder.AddUserSecrets<Program>();
                }
            })
            .Build();
        
        host.Run();
    }
}

I've read through the migration documentation and I can't seem to find anything that addresses this issue, so I guess in the meantime I will just migrate over to using Program.cs. If these findings are correct, it would be helpful to add an annotation to the documentation that clarifies this issue. However, I assume there is a way to do this that I just can't figure out.


Document Details

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

fceci90 avatar Jul 29 '22 13:07 fceci90

Access to secret manger is just part of configuration, can you get other configuration providers to work?

Rick-Anderson avatar Jul 29 '22 21:07 Rick-Anderson

The only other location I read configuration from is appsettings.json and that works just fine.

fceci90 avatar Aug 01 '22 13:08 fceci90

Thank you for contacting us. Due to a lack of activity on this discussion issue we're closing it in an effort to keep our backlog manageable. If you believe there is a concern which hasn't been addressed, please file a new issue.

Rick-Anderson avatar Dec 20 '22 19:12 Rick-Anderson