Search code examples
asp.net.netazureenvironment-variablesazure-app-configuration

Can i read environment variable from Azure App Settings by using Environment.GetEnvironmentVariable method?


I am adding Firebase SDK to the server.
The first step is to set the GOOGLE_APPLICATION_CREDENTIALS environment variable,
so I set the environment variable in the Azure Service -> Configuration -> App Setting.
I checked environment variables from kudu too.
And I tried to get an environmental variable from my ASP.NET app using Environment.GetEnvironmentVariable method,
but It returns null and all the other environmental variables of the app settings return null too.
My project is built with .NET Framework 4.8 and MVC5.
Is there any information about this?

using Microsoft.Ajax.Utilities;
using Microsoft.AspNet.Identity;
using Nest;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Linq;
using System.Net.Mail;
using System.ServiceModel.Channels;
using System.ServiceModel.Syndication;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace ---.Controllers
{
    public class HomeController : Controller
    {
        public async Task<ActionResult> Index(ActionParams param)
        {
            ...

            var appsettings = ConfigurationManager.AppSettings;
            var local = appsettings["GOOGLE_APPLICATION_CREDENTIALS"];
            var production = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
            ViewBag.localEnv = local;
            ViewBag.productionEnv = production;

            return View("Home", "Index");
        }

This project is from my company, so i blocked some information and the language is korean.

enter image description here


Solution

  • I am able to get the Azure Environment variables in my Environment. Please check the below workaround.

    • Created a .NET Framework App with MVC.
    • Installed the below NuGet Packages
    Microsoft.Configuration.ConfigurationBuilders.AzureAppConfiguration
    Microsoft.Configuration.ConfigurationBuilders.Environment
    System.Configuration.ConfigurationManager
    

    Steps to Install NuGet Package

    Right Click on the Solution Explorer => Manage NuGet Packages => Search in the Browse tab

    enter image description here

    • In Web.config file => Under Configuration section add the below settings
     <configSections>
        <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
      </configSections>
      <configBuilders>
        <builders>
          <add name="DefaultEnvironment" mode="Greedy" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment" />
        </builders>
      </configBuilders>
     
      <appSettings configBuilders="DefaultEnvironment">
        <add key="GOOGLE_APPLICATION_CREDENTIALS" value="Set via an environment variable - for example, dev, test, staging, or production connection string." />   
      </appSettings>
    
    • Add the New App setting in Azure Web App Configuration Section. enter image description here

    • Initially local web.config file contains the appsettings with some default value. Now I will override the value with Azure App Service Setting.

    • In HomeController, write the below code.

     public ActionResult Index()
            {
                var appsettings = ConfigurationManager.AppSettings;
                var local = appsettings["GOOGLE_APPLICATION_CREDENTIALS"];
                var production = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
                ViewBag.GOOGLE_APPLICATION_CREDENTIALS = local;
                ViewBag.GOOGLE_APPLICATION_CREDENTIALS1 = production;
                return View();
            }
    
    • I am able to get the Environment variable by using both
    var appsettings = ConfigurationManager.AppSettings;
    appsettings["GOOGLE_APPLICATION_CREDENTIALS"];
    and 
    Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
    
    • In Home => Index.cshtml, add the below snippet somewhere in the file
     <h2> @ViewBag.GOOGLE_APPLICATION_CREDENTIALS - Local Settings</h2>
     <h2> @ViewBag.GOOGLE_APPLICATION_CREDENTIALS1 - Production Settings</h2>
    

    Local App Output enter image description here

    Published App OutPut : enter image description here

    • As you can see local settings are overridden by Azure App Settings.

    References taken from MSDoc