How can I have Azure App Service environment variables override key vault secrets?

David Thielen 2,706 Reputation points
2024-07-09T22:05:52.75+00:00

Hi all;

If I define a string in both the Key Vault and the Azure App Service environment variables, the app will read the Key Vault entry.

Is there a way to have it read the App Service value if set?

thanks - dave

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,491 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,318 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. TP 83,051 Reputation points
    2024-07-09T23:21:58.4666667+00:00

    Hi Dave,

    Each environment variable must have a unique name, so either the variable will be sourced from key vault via reference or app service, depending on how you set it. Based on this I'm unsure what you are asking.

    You could of course build your own abstraction that would do what you want. For example, you could have code that would check for existence of VARIABLENAME_OVERRIDE and if it exists use that instead of the default behavior.

    -TP


  2. Bruce (SqlWork.com) 60,866 Reputation points
    2024-07-10T15:32:40.2933333+00:00

    The order of precedence of config providers is the opposite order of adding. That is the last one wins. To control the order, you need to manually add the providers in the order you want rather than the default.

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-8.0


  3. Bruce (SqlWork.com) 60,866 Reputation points
    2024-07-10T22:36:00.07+00:00

    instead of using the default, add the configuration providers in your code.

    // defaults
    var builder = WebApplication.CreateBuilder(args);
    
    // remove config sources
    builder.Configuration.Sources.Clear();
    
    // add config providers in order you want:
    builder.Configuration.AddEnvironmentVariables();
    ...
    
    0 comments No comments