0

I created an environment variable in Azure App Service. However, I'm not able to pick the value from Azure while it is published.

So I added it to the appsettings.json, and it works.

My question would be, if I add an environment variable in Azure configuration settings, shall I add it in the appsettings.json as well, or is having it in the Azure environment settings enough?

enter image description here

When I navigate to

https://your-web-name.scm.azurewebsites.net/Env.cshtml

I can clearly see the variable is present there. Why is this not being picked up in the code? Am I doing something wrong?

appSettings["MailKitPassword"] <-- This is not being picked up, so I have to hard-code it.
bubbleking
  • 3,329
  • 3
  • 29
  • 49
Csibi Norbert
  • 780
  • 1
  • 11
  • 35

2 Answers2

2

In order to retrieve it you should use Environment.GetEnvironmentVariable("APPSETTING_MailKitPassword")

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
2

As Thiago Mentioned, you need to use GetEnvironmentVariable method to retrieve the AppSettings values,

enter image description here

so your code should be

Environment.GetEnvironmentVariable("APPSETTING_MailKitPassword")

However i would recommend you to store the passwords in Azure KeyVault.

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396