Azure App Service configuration settings (environment variable) does not work

1.5k Views Asked by At

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.
2

There are 2 best solutions below

0
On BEST ANSWER

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

1
On

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.