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");
}
I am able to get the Azure Environment variables in my Environment. Please check the below workaround.
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
<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.
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();
}
var appsettings = ConfigurationManager.AppSettings;
appsettings["GOOGLE_APPLICATION_CREDENTIALS"];
and
Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
<h2> @ViewBag.GOOGLE_APPLICATION_CREDENTIALS - Local Settings</h2>
<h2> @ViewBag.GOOGLE_APPLICATION_CREDENTIALS1 - Production Settings</h2>
Local App Output
Published App OutPut :
References taken from MSDoc