3

I have an ASP .Net Core 2.2 Web API which connects to a MySQL Database. It is hosted on Azure App Service.

Currently I am storing the connection strings in the appsettings.json file:

"ConnectionStrings": {
    "MyDataContext": "Server=server1.mysql.database.azure.com;user id=username;Pwd=password;persistsecurityinfo=True;database=db1;"
  }

And I read the connection strings in Startup.cs (in the ConfigureServices method) like this:

services.AddDbContext<MyContext>(options => options.UseMySql(Configuration.GetConnectionString("MyDataContext")));

I read that I can store these connection strings in the Azure App Service Settings instead, as it offers some advantages. I've been trying to do this, but am struggling to get it to work.

My first question is, when adding the connection string to Azure App Service Settings (under the Connection Strings section), what do I put in for Name in the Name/Value pairs? Do I just put "MyDataContext" or do I put in "ConnectionStrings:MyDataContext"? (I'm trying to keep the same structure as I have in the appsettings.json file). My second question is, can I still read the connections string in my Startup.ConfigureServices method the same way I am doing now? From what I understand, Azure App Service will automatically inject the connection string store in Settings into the Configuration object in the API? I'm not sure if I'm missing something, but it's not working...

enter image description here

Fabricio Rodriguez
  • 3,769
  • 11
  • 48
  • 101
  • 1
    1. Just use the "inner" key value, so `MyDataContext` for the "name" on AppServices. 2. Yes, you can keep reading the connection string as you do already in your `Startup`. It should all work. – jpgrassi Aug 23 '19 at 11:13
  • 1
    Thanks jpgrassi. And I would assume that after adding the setting in the Azure App Service, I can delete it from my project's appsettings.json? – Fabricio Rodriguez Aug 23 '19 at 11:23
  • Why would you want to delete it? The values are replaced by Azure, and injected as environment variables to your app. Unless you used some custom way of loading your settings in `Program.cs` all should work. But a usual use case is: `appsettings.json` has a `ConnectionStrings` but with a empty value. Then you have the same on `appsettings.development.json` with a dev connection string. The production one you use via env variables, from your Azure App Service. – jpgrassi Aug 23 '19 at 11:26
  • Thanks jpgrassi. Looks like there's a bit of a delay after editing the Settings in the Azure App Service before they take effect. A minute or so. It's working now. By the way, I complete deleted the "ConnectionStrings" section in the appsettings.json file and it still works - looks like Azure App Service is injecting the connection strings into the Configuration object, even with no "ConnectionStrings" section in my appsettings.json file – Fabricio Rodriguez Aug 23 '19 at 11:38
  • 1
    Yes, that's how it works. It will inject the connection string in the format of `ConnectionStrings:MyDataContext`, so your app can read it as it was. This article is highly recommended in case you want to learn more about the configuration system in ASP.NET Core: https://www.paraesthesia.com/archive/2018/06/20/microsoft-extensions-configuration-deep-dive/ – jpgrassi Aug 23 '19 at 11:58
  • @jpgrassi you would want to delete it from the appsettings.json file so that you're not storing database connection credentials in your source code. – ataraxia Aug 23 '19 at 17:16
  • Well, you shouldn't be doing that anyway :) – jpgrassi Aug 23 '19 at 17:17
  • You asked why you'd want to delete it, that's the question I was answering, it's more secure not to have any connection strings in the project's appsettings.json file. – ataraxia Aug 23 '19 at 17:19
  • Also, I believe the `ConnectionStrings:MyDataContext` syntax is not used in the current version (RC2), it is now `SQLCONNSTR_MyDataContext`, though it is still called in the code as `Configuration.GetConnectionString("MyDataContext")`. You can find this via the portal by opening your app service and going through Development Tools -> Advanced Tools -> Go -> Environment (tab at the top). This should show all stored app settings, connection strings and environment variables being stored in Azure for your app. – ataraxia Aug 23 '19 at 17:20

2 Answers2

2

Yes and yes.

Yes, you can name the connection string as almost anything you want, though I've discovered recently that certain characters aren't allowed, but Azure doesn't tell you this, it will just strip them out and not tell you, so to be safe I'd only use alphanumeric characters, you can use CamelCasing if you need to. You don't need to prefix with anything like ConnectionStrings: as this is done automatically by Azure, and the syntax has changed in the most recent version anyway.

And yes, if you haven't specified your own ConfigurationBuilder, your web app should call CreateDefaultBuilder which will add environment variables at runtime. The code you've written will stay the same and you can delete the connection string from your appsettings.json file, if you keep it in there it will get overridden anyway.

It is a good idea to remove connection strings from the appsettings.json file if you are able to store them in Azure instead for security reasons, so that you are not storing either database server address nor connection credentials in your source code. One step up from this is using Managed Identity in conjunction with Active Directory, where you specify an AD username in the connection string but no password, then assign that user (or the user's group) as the server admin.

ataraxia
  • 995
  • 13
  • 31
0

It looks like there's a slight delay (a couple of seconds at least) when changing the Settings of an Azure App Service - I was too quick...

Fabricio Rodriguez
  • 3,769
  • 11
  • 48
  • 101