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:

image

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

image

You can also choose the connection type:

image

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

image

Result in Azure website

image

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

image

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

info Last modified by Raymond at 3 years ago * This page is subject to Site terms.

More from Kontext

Entity Framework Core Code-First - Generate Covering Index with Columns Included

local_offer entity-framework local_offer asp.net core local_offer Azure local_offer C#

visibility 17
thumb_up 0
access_time 7 days ago

In SQL Server or some other relational databases, it is a very common requirement to create covering index with columns included in index pages beside the index key columns. With Entity Framework Core, you can also easily generate covering indexes using purely C# code. Scenario For ...

open_in_new ASP.NET Core

local_offer Azure local_offer kontext

visibility 9
thumb_up 0
access_time 3 months ago

In the past few months, Kontext's DNS server was not very stable as it went offline several times accidentally. When the DNS server is down, our domain (kontext.tech) cannot be parsed successfully to the IP address of Azure App Service (the service that hosts this website); thus it became unreach...

open_in_new Kontext Information

Modern Web Application - Azure Blob Storage for Uploaded Files

local_offer Azure local_offer asp.net core local_offer dotnetcore

visibility 274
thumb_up 0
access_time 3 months ago

With cloud platforms like Azure, we can totally separate user content storage from web application storage to decouple components from each other and to make the application easy to scale and deploy. This article provides detailed information with code snippets about how to use Azure server-less product Blob Storage and App Service to enable horizontally scalable web application for users to upload files (BLOBs).

open_in_new Azure

Azure SQL Database Automated Backup Strategy

local_offer Azure local_offer SQL Server

visibility 78
thumb_up 0
access_time 3 months ago

When designing the architecture of Kontext platform, Azure SQL Database is chosen as the storage for relational data. TDE and other advanced security features are always enabled to protect the database. Backup plans are also employed to ensure I can always restore the database for as point of tim...

open_in_new Azure

info About author

comment Comments (0)

comment Add comment

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

No comments yet.

Dark theme mode

Dark theme mode is available on Kontext.

Learn more arrow_forward

Kontext Column

Created for everyone to publish data, programming and cloud related articles. Follow three steps to create your columns.


Learn more arrow_forward