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

David Thielen 3,116 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,577 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,758 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 65,206 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();
    ...
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 65,206 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://video2.skills-academy.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.