1

I use the secrets.json file to store credentials for my Blazor .Net Core 3.0 Web App and use IConfiguration to get the values like this

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
    var username = Configuration["Account:Username"];
    var password = Configuration["Account:Password"];
    new Account(username, password);
}

public IConfiguration Configuration { get; }

Which works fine on my machine in Development, but when I switch to Release in my app Properties > Debug > Environment variables > ASPNETCORE_ENVIRONMENT, the secrets.json doesn't get loaded. Same when I upload the app to Azure, where I have set up the variables as Application settings, it doesn't work. What am I missing here? How can I set up the Web App in Production on Azure, so that I can use the credentials?

edit:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
mKay
  • 301
  • 1
  • 3
  • 15
  • 1
    How have you setup your host builder? The default builder should add the environment variables provider that adds App Service settings to the config. Remember that those settings need to be named `Account:Username` or `Account__Username`. – juunas Jan 14 '20 at 06:16
  • I edited my initial post, is it the correct method? The variables in the Application settings are named exactly as in the secrets.json – mKay Jan 14 '20 at 09:17
  • Well it looks okay :\ The reason the secrets don't load by the way is that the default builder only adds them if ASPNETCORE_ENVIRONMENT is Development. – juunas Jan 14 '20 at 10:02
  • I just tried the following command in the debug console: `set | findstr Account:Username` which I found here https://stackoverflow.com/a/34622196/5899174. This command finds two values, one `Account:Username=....` and one `APPSETTING_Account:Username=....` so I guess this is correct. But in the SO question of the link, the person says it uses `GetEnvironmentVariable(...)` to get the variable, but I use `Configuration[...]` does this make a difference? – mKay Jan 14 '20 at 10:56
  • Okay, it works now. I just deleted the Web App from Azure and recreated it. Then added the Application settings and now it works. I guess I made some changes somewhere in the Azure settings sometime in the past. – mKay Jan 14 '20 at 11:58

0 Answers0