Can Get-AzWebApp get connection string information if held separate from web.cofig - azure

I'm trying to use the Get-AzWebApp PowerShell command to get a list of azure app services and then read the configuration/connection strings from each one. Does this still work if the web.config of each app service has the connection strings stored in a separate file, so e.g. in web.config we have
<connectionStrings configSource="App_Config\ConnectionStrings.config" />
Would we expect this to work still?

Does this still work if the web.config of each app service has the connection strings stored in a separate file, so e.g. in web.config we have
<connectionStrings configSource="App_Config\ConnectionStrings.config" / >
Would we expect this to work still?
Currently it is not possible. Fetch the connection strings from files is not possible in an app service.
In a PowerShell we cannot edit or add bulk connection strings and app settings using files.
We can add the connection String/ App Settings it will be added like key value. So, whatever we add in an App Settings we can get that as string value.

Related

Storing connection strings in Azure App Service Settings

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...
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.
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...

Azure - WebJobs - Use remote connection string

I have WebJobs running under my Azure Web App. For Web App you can set that the Web App will use remote connection strings (that you setup on Azure portal).
Is it possible to do the same for WebJobs?
So they would be looking for remote connection string instead of using a connections string from (for example) "app.config".
Could you add the connection string as Web Config value that can be accessed by either the site or webjob?
Further more details you could refer to this blog:Configuring Azure Web Jobs.
The main steps are as follows:
Install CloudConfigurationManager package Microsoft.WindowsAzure.ConfigurationManager
Set your connection strings to the app setting.
Retrieve connection string using the CloudConfigurationManager:
var myConnectionString = CloudConfigurationManager.GetSetting("MyConnectionString");
I found out that a connection string setup on Azure Portal overwrites connection string with the same name in .config files. Which means that no additional setup is required.
Azure Portal Connection string takes priority over local *.config connection string.
I successfully tested this.

How can I view the final appSettings values on an Azure App Service web app?

I have an ASP.NET MVC app deployed to Microsoft Azure App Service and am having some trouble with the appSettings and connectionStrings values.
I have some values set in the web.config and some values overriding them in the Application Settings tab of the App Service. I want to quickly and easily view the final values to check that the settings are being picked up correctly.
How can I do this?
Note: I've tried using az webapp config appsettings list but this only seems to bring back what is configured in the Application Settings of the App Service and not the merged results of combining with web.config.
No Azure API will return values that include settings that come from your web.config file.
The only way to get this is to ask the config system within your own runtime. e.g. Use code along these lines:
foreach (string name in ConfigurationManager.AppSettings)
{
string val = ConfigurationManager.AppSettings[name];
...
}
foreach (ConnectionStringSettings settings in ConfigurationManager.ConnectionStrings)
{
string connStr = settings.ConnectionString;
string provider = settings.ProviderName;
...
}
This will give you the effective values that are applied to your app.
You may also use the following blades in Azure Portal (under Development Tools section):
Console
In order to see the file, you may use type command, e.g.:
type web.config
Advanced Tools
This points to the Kudu service.
You may see files deployed when navigating to Debug Console > Choose either CMD or PowerShell. Then navigate to your config directory (e.g. site/wwwroot) and choose to either download or edit file.
App Service Editor
App Service Editor is a relatively new tool in Azure toolset. Default view is a list of files, so you can browse all hosted files, including configuration ones.
You can view all of your runtime appSettings, connection strings and environment variables (and more..) using azure KUDU SCM. if your application address is "https://app_name.azurewebsites.net" you can access it in the address "https://app_name.scm.azurewebsites.net" or from azure portal
With kudo REST API, you can get the settings, delete or post them in this address https://app_name.scm.azurewebsites.net/api/settings
kudo wiki

Azure Application Settings not overriding my appsettings.json file values

I have tried adding DefaultConnection from my appsettings.json file to Azure's Application Settings but Azure will not override the connection string.
Any article or blog I can find states that all I should need to do is add the connection string name as it states in the appsettings.json file and Azure should do the rest (e.g. https://tehremo.wordpress.com/2016/10/07/override-connection-strings-app-settings-in-asp-net-core-and-azure-app-service/) however when the application is published it is using my local connection string.
My Startup.cs file looks like the following:
NOTE: I am publishing using VSTS continuous delivery with "Deploy Azure App Service" release task.
I just had a similar problem (the problem was with PostgreSQL connection string type, I had to change it to custom) and now it works for me, so these are the pieces:
This is my appsettings.json file. I have a value for 'Psql' set in my appsettings.Development.json, but in the appsettings.json it is left empty.
These are the settings which are set in the Azure portal. Please note, that there are two ways to override the connection string.
This is the part of my Startup.cs file. Pay attention to the order of how the settings are applied in the Startup constructor and the way I get the connection string in the ConfigureServices method (GetConnectionString is a standard extension method).
Additional info from my comments below:
Azure GUI (Connection strings, Application settings) uses environment variables internally, so the appsettings.json will stay the same.
If there is a need for an appsettings.json's value to be overwritten during VSTS release activity (before it will be published to Azure), Colin's ALM Corner Build & Release Tools can be used. Here are the links to Colin's ALM Corner Build & Release Tools and tutorial.
Thanks #pasul, your help was much appreciated and helped me find an alternative solution. In order to deploy using VSTS task and replace application settings, you will need to add variables to the release task and pass into the task the json file in question for variable substitution.
When in "Deploy Azure App Service" release task you should see a "File Transforms and Variable Substitution" section. In here you will supply the path to the json file you want to swap variable values.
Then you will need to click on the options button on the release environment. You will see an option to configure variables in the pop out menu.
From here you can add the json property you want to modify as a variable. In my case the connection string. Which will look like the following:
"ConnectionStrings.DefaultConnection"
Then just put in your connection string value. VSTS will then swap out these values for you when deploying.

azure WEBSITE_NAME appsetting is missing

Reading through the azure documentation and various posts on here, i understand there should be a number of settings available to web apps running on azure, among them WEBSITE_HOSTNAMEand WEBSITE_SITE_NAME. These should also overwrite any existing configuration appsettings with same key.
When i attempt to run my app, it is reading the key from the config file (i.e. its not being overwritten by azure). If i remove the value from the config, i get an exception about not being able to pick up a config value.
Is there a step im missing? Are these values only available at certain tiers?
Those values are only available as environment variables, so you'll need to read them from there.
App settings set in the Web App blade override settings and become env variables, but these are just environment variables.

Resources