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

David Thielen 3,211 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

Developer technologies | .NET | Blazor
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,965 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    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();
    ...
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.