Just as all Azure Web Apps need configuration values, most applications also need to have database Connection String values configured. With Azure Web Apps the Connection Strings are stored/retrieved in a very similar fashion as Azure Web App Application Settings. Connection Strings are also Key / Value pairs of String values, but are separated out into their own section.

Connection Strings are typically used to store the connection information for one or more databases the Web App needs to connect to for storing and retrieving data. The Connection String types supported are SQL Database, SQL Server, MySQL and Custom. Most often the Connection Strings used will be for some kind of SQL RDMS, but the Custom type allows for an additional Connection String to be configured any other type of database connection necessary.

As with Application Settings, the Connection Strings are accessed as normal from .NET code and the values will come from what is set within the Azure Management Portal. In other development environments (Node.js, Java, PHP, Python) the Connection Strings are exposed to code as Environment Variables. Additionally, the Connection Strings are editable within the Azure Management Portal, but are read-only when access through code.

Connection String within Azure Management Portal

Managing the Connection Strings for an Azure Web App is performed through the Azure Management Portal.

Here are the necessary steps for locating and accessing the Connection Strings for an Azure Web App within the portal:

  1. Open the Azure Management Portal via https://portal.azure.com
  2. Navigate to the Web App within the portal.
  3. Under “All settings” open up the “Application settings” pane
  4. Scroll down to the “Connection strings” section

AzureWebApp_ConnectionStrings

Connection Strings from .NET

Retrieving the Connection Strings for the Azure Web App is performed from .NET using the ConfigurationManager class in a very similar fashion as how Application Settings are retrieved.

Here’s an example of retrieving a Connection String from C# code:

using System.Configuration;

var key = "MyDBConnString";
string value = ConfigurationManager.ConnectionStrings[key]
    .ConnectionString;

Connection Strings from Node.js

In Node.js the Azure Web App Connection Strings are exposed as Environment Variables. Accessing the Connection Strings through the Environment Variables is exactly like accessing Application Settings, but with a different string prefixing the Connection String key name.

Here are the Connection String Environment Variable key name prefixes:

  • SQL Database prefix is “SQLAZURECONNSTR_”
  • SQL Server prefix is “SQLCONNSTR_”
  • MySQL prefix is “MYSQLCONNSTR_”
  • Custom prefix is “CUSTOM_CONNSTR_”

Here’s an example of retrieving a Connection String from Javascript running on Node.js:

var value = process.env.SQLCONNSTR_MyDBConnString;

Connection Strings from Java

Azure Web App Connection Strings are exposed to Java code as Environment Variables. These are accessed in the same method as Application Settings except with different string prefixes on the Connection String key names.

Here are the Connection String Environment Variable key name prefixes:

  • SQL Database prefix is “SQLAZURECONNSTR_”
  • SQL Server prefix is “SQLCONNSTR_”
  • MySQL prefix is “MYSQLCONNSTR_”
  • Custom prefix is “CUSTOM_CONNSTR_”

Here’s an example of retrieving a Connection String from Java code:

String value = System.getenv("SQLCONNSTR_MyDBConnString");

Posted by Chris Pietschmann

Chris is a Microsoft MVP and has nearly 20 years of experience building enterprise systems both in the cloud and on-premises. He is also a Certified Azure Solutions Architect (both MCSD and MCSE), a trainer, and Cloud Advocate. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.

One Comment

  1. […] feature of Azure Web Apps that helps with this is the ability to override application settings and connection strings from within the Azure Portal. This way you could allow your developers or infrastructure admins to […]

    Reply

Leave a Reply