Retrieve Azure AppSettings and Connection String Settings in ASP.NET Core Apps

Raymond Tang Raymond Tang 0 17142 6.08 index 10/10/2017

Background

In ASP.NET Core, we can easily use user secrets to manage our password or credentials. This post will summarize the approaches we can use after the websites are deployed into Azure.

Azure AppSettings & Connection Strings

In Azure management portal, navigate your App Services. And then in SETTINGS section, click on Application settings. In App settings section, you can setup up any key way pairs:

https://api.kontext.tech/resource/2bc68972-85bf-5649-b30e-882d49979a4f

In Connection strings section, you can setup all the connection strings:

https://api.kontext.tech/resource/339d0599-3c01-51c8-8026-50e69c4c7175

You can also choose the connection type:

https://api.kontext.tech/resource/53859c4a-066f-5b06-89cf-dfa38ad94026

These settings are injected at run time.

Access App settings and Connection strings in your application

IConfiguration

All the configurations will be injected and can accessed through IConfiguration. You can directly add IConfiguration into your controller or services. You can also inject this directly into your views.

Sample code in Controller

using AzureWebAppTest.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System.Diagnostics;

namespace AzureWebAppTest.Controllers {      public class HomeController : Controller      {          private readonly IConfiguration configuration;

public HomeController(IConfiguration configuration)          {              this.configuration = configuration;          }

public IActionResult Index()          {              var appSettingTest1 = configuration["appSettingTest1"];              var connectionStringTest1 = configuration.GetConnectionString("connectionStringTest1");

return Json(new { appSettingTest1, connectionStringTest1 });          }

}

Sample configuration in Azure

https://api.kontext.tech/resource/21708e2e-2e2d-5fe1-a12a-f8e0cb4dbb7b

Result in Azure website

https://api.kontext.tech/resource/ce048143-c17b-56b8-bd9f-99a93e682d5a

Sample code in View

@{      ViewData["Title"] = "Home Page";      Layout = null; } @using Microsoft.Extensions.Configuration @inject Microsoft.Extensions.Configuration.IConfiguration configuration

<p>appSettingTest1: @configuration["appSettingTest1"]</p> <p>connectionStringTest1: @configuration.GetConnectionString("connectionStringTest1")</p>

Result in Azure website

https://api.kontext.tech/resource/510db773-9fb8-5084-a1c8-c465a40391e0

Access through environment variables

All these settings are injected into environment variables and you can access them in other programming languages by adding some prefix:

For App settings, ‘APPSETTING_’ is added to each settings. and for connection strings, there are different prefixes added for different connection types:

  • SQL Server: SQLCONNSTR_
  • MySQL: MYSQLCONNSTR_
  • SQL Database: SQLAZURECONNSTR_
  • Custom: CUSTOMCONNSTR_

For more details, please refer to: https://docs.microsoft.com/en-us/azure/app-service/web-sites-configure

Update these settings in CI/CD environment

Instead of manually updating these settings through Azure management portal, you can also Azure Resource Manager PowerShell module to implement it in a continuous integration and deployment environment. Refer the following post for examples: https://blogs.msdn.microsoft.com/tfssetup/2016/05/20/accessingupdating-azure-web-app-settings-from-vsts-using-azure-powershell/

VSTS

If you are using Visual Studio online build and release functions, you can also just install some extensions available in Visual Studio marketplace. For instance, the following link provide you the functionality to configure App settings:

https://marketplace.visualstudio.com/items?itemName=hboelman.AzureAppServiceSetAppSettings

azure dotnetcore

Join the Discussion

View or add your thoughts below

Comments