5

It is possible in Azure Web App to override web.config AppSettings section easily. E.g. if I have the following web.config:

<appSettings>   
    <add key="AllowedCORSOrigin" value="http://localhost:26674"/>
</appSettings>

I can override it in the app settings UI in the portal like that:

enter image description here

I have also a custom section in the web.config:

<AdWordsApi>
    <add key="OAuth2RefreshToken" value="TOKEN" />
</AdWordsApi>

Is it possible to override it somehow as well? I have tried AdWordsApi.OAuth2RefreshToken and AdWordsApi:OAuth2RefreshToken, but that does not work that easily.

P.S. It's interesting to know if it's possible with other custom sections like e.g if I want another authentication mode on the server.

<system.web>
    <authentication mode="None" />
</system.web>
Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207

5 Answers5

5

Short answer is that it is not possible.

The mechanism you describes only works with App Settings and Connection Strings. High level, the way it works is:

  • Your Azure App Settings become environment variables
  • At runtime, a special module sets those dynamically in the .NET config system. Note that the physical web.config is never modified.

But it would be hard to make such mechanism work on arbitrary config sections, as those could not be dynamically affected without modifying the physical file.

David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • 1
    Any solutions for the problem? Not publishing web.config (and modifying it directly in Azure) or using web.config transformations? As far as I remember it works rather fine for ASP.NET Core with A.B.C syntax, but it has simpler json config :) – Ilya Chernomordik Oct 28 '16 at 18:13
  • If you're publishing via git, you could conceivably have a build step which does token replacement on your web.config file. That means you'd need to redeploy when you change your settings, but at least you'd keep the secrets only in Azure. – David Ebbo Oct 28 '16 at 18:15
  • Ok I see, thank you very much for explanation. Am I right that it is supported for ASP.NET Core and we can override any value in settings in this manner? – Ilya Chernomordik Oct 28 '16 at 18:20
  • Not an expert on Core, but it will work as long as the config system is consuming environment variables as a way to override hardcoded values. – David Ebbo Oct 28 '16 at 18:21
  • Right, it can do that directly without a special injection. Well, one more argument to start using .Net Core :) – Ilya Chernomordik Oct 28 '16 at 18:23
  • 1
    For those wondering, the "special module" is called `EnvSettings` and it only exists in the GAC. You can use Kudu to copy `"D:\Windows\Microsoft.Net\assembly\GAC_MSIL\EnvSettings\v4.0_1.0.0.0__31bf3856ad364e35\EnvSettings.dll"` to your local machine so you can poke inside of it to see how it works. – Dai Jun 26 '20 at 04:32
2

If you are using Visual Studio use web.config transformations to change configuration settings depending on whether you are running locally or deploying to Azure:

How to Transform Web.config

In simple terms you create one more more build configurations (typically Debug & Release). In your Visual Studio solution right-click on your existing web.config file and click "Add Config Transform", this will create a Web.Debug.Config and Web.Release.Config file which you can then customise with specific settings depending on the environment. Link this with your Azure build configuration and you can then have any combination of settings for local and remote deployment.

connectedsoftware
  • 6,987
  • 3
  • 28
  • 43
  • I see that this is possible to achieve this way, but I have some "secret" stuff e.g that I would like to put into Azure and not have it locally or in source control. So while this solution works, I would prefer to use Azure Environmental Variables if possible from the portal. Thanks for the answer nontheless – Ilya Chernomordik Oct 28 '16 at 09:56
0

This is old but leaving this reference to how to use Azure Resource Manager to potentially solve this.

DreadedFrost
  • 2,602
  • 1
  • 11
  • 29
0

You can transform the values by the listed in VSTS by doing the following steps in App.Release.config:-

  1. Add xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" in configuration section
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    </configuration>
  1. Add xdt:Transform="Replace" in custom section like below
<AdWordsApi  xdt:Transform="Replace">
    <add key="OAuth2RefreshToken" value="TOKEN" />
</AdWordsApi>
  1. Create variable token in the release pipeline e.g OAuth2RefreshToken

  2. Then in file config use it as following

<AdWordsApi  xdt:Transform="Replace">
    <add key="OAuth2RefreshToken" value="#{OAuth2RefreshToken}#" />
</AdWordsApi>
0

If you are adding any in web.config --> Appsetting, you can overirde it in Azure App Service using variable prefix

Key Name: APPSETTING_AllowedCORSOrigin Value: http://localhost:26674

https://learn.microsoft.com/en-us/azure/app-service/reference-app-settings?tabs=kudu%2Cdotnet#variable-prefixes