Azure App Configuration for ASP.NET: A Complete Guide to Centralized Configuration Management
Externalizing properties (or configurations) in an ASP.NET application to an Azure App Configuration store allows for centralized configuration management, easier updates, and improved security. Here's a complete guide to set this up:
Step 1: Create an Azure App Configuration Store
- Log in to Azure Portal.
- Search for "App Configuration" in the search bar and click on it.
- Click on "Create" and provide the following details:
- Subscription: Select your Azure subscription.
- Resource Group: Select an existing resource group or create a new one.
- Name: Provide a unique name for the App Configuration store.
- Region: Choose your preferred region.
- Click "Review + Create" and then "Create".
Step 2: Add Configuration Settings in Azure
- Go to your App Configuration store in the Azure Portal.
- Navigate to "Configuration Explorer".
- Click "Create" to add a new key-value pair.
- Key: The name of your configuration property (e.g.,
AppSettings:DatabaseConnectionString
). - Value: The value of the configuration property (e.g., your connection string).
- Label: Optionally, add a label to distinguish different environments (e.g.,
Development
,Production
).
- Key: The name of your configuration property (e.g.,
Step 3: Add NuGet Packages in ASP.NET Application
- Open your ASP.NET project in Visual Studio.
- Add the following NuGet packages:
Microsoft.Extensions.Configuration.AzureAppConfiguration
Microsoft.Azure.AppConfiguration.AspNetCore
Use the Package Manager Console or NuGet Manager UI to install them:
Step 4: Register Azure App Configuration in Startup
- In your
Program.cs
orStartup.cs
(depending on your project type), modify the configuration setup:
For .NET Core 6.0 and later:
For .NET Core 3.1 and earlier:
Step 5: Connect to Azure App Configuration
Obtain the connection string for your App Configuration store:
- Go to your App Configuration store in Azure.
- Navigate to "Access Keys".
- Copy the Connection String.
Add the connection string to your local development environment's
appsettings.json
orsecrets.json
file:Alternatively, use an environment variable for production.
Step 6: Access Configuration Properties in Code
You can now access your configurations using IConfiguration
:
Example:
Step 7: Enable Dynamic Configuration Updates (Optional)
To enable dynamic configuration updates without restarting your app:
Modify your configuration setup in
Program.cs
:Use
IOptionsSnapshot<T>
for dynamically updated configurations in your services.
Step 8: Secure Access with Managed Identity (Recommended)
Instead of using a connection string, you can use Azure Managed Identity:
- Assign a Managed Identity to your Azure App Service or Virtual Machine.
- Grant the identity "App Configuration Data Reader" role in the App Configuration store.
- Modify your configuration setup:
Your ASP.NET application is now configured to use Azure App Configuration for centralized property management. This setup allows you to manage settings dynamically, implement feature flags, and maintain security using best practices like Managed Identity.