2

I have an appsettings.json in my ASP.NET Core 6 Web API and a secrets.json I use for development connection strings, like below:

{
  "ConnectionStrings:MainDatabase": "CONNECTION STRING HERE"
}

After publishing my API as an Azure Container App, how can I override these connection strings from Azure?

I see the "Secrets" settings to configure a KeyVault, but how can I link those secrets to the User Secrets/appsettings used by my Web Api?

Fabio
  • 61
  • 6
  • 1
    have you check this article, it explain how secrets can be bound to env variables:https://learn.microsoft.com/en-us/azure/container-apps/manage-secrets?tabs=arm-template. /in you case you would have an env variable `ConnectionStrings:MainDatabase` that would referenced the related container app secret. – Thomas Oct 14 '22 at 20:49

1 Answers1

4

After updating or changing the User Secrets we need to restart or recreate the revision.

enter image description here

My Secrets: enter image description here

Reading Secret Key:

public IActionResult Index()
{ 
  ViewBag.MyKey= Environment.GetEnvironmentVariable("mykey");
  return View();
}

Make sure the name of the key must be same as the local Secret name.

Even if you have secrets created in the Secrets section of the Azure Container App, you can see the Environment variables in Container Section is unavailable.

enter image description here

For the secrets to work, we need to map the secrets into the Environment variables.

  • Click on Edit and deploy

enter image description here

Create new Revision

Click on the container image => Create Environment variables with mapping to the existing secrets =>Save => Refresh

enter image description here

Every time you make changes to the secrets in portal, If you don`t have the revision we need to create it or restart the existing Revision.

Use the below command to restart the revision.

az containerapp revision restart --resource-group "yourRG" --name "containerappname" --revision "revisionname"

enter image description here

Harshitha
  • 3,784
  • 2
  • 4
  • 9