Skip to main content

Command Palette

Search for a command to run...

Retrieve configuration settings from Azure App Configuration

Published
6 min read
Retrieve configuration settings from Azure App Configuration
C

Have dual bachelor degrees in BSc in Control Engineering and Computer Engineering and MSc in Artificial Intelligence. Have worked as .net fullstack/ backend developer almost 5 years now. Passionate about both Software and AI Engineering. Specializing in Azure Cloud currently.

In this article, we will create an Azure App Configuration resource, store configuration settings using Azure CLI, and build a .NET console application that uses the ConfigurationBuilder to retrieve configuration values. We will also learn how to organize settings with hierarchical keys and authenticate our application to access cloud-based configuration data.

Create an Azure App Configuration resource and add configuration information

Navigate to portal.azure.com and sign in using Azure credentials. Use se the [>_] button to open Azure cloud shell and choose Bash. If it asks you persistence, choose No storage account required, your subscription and then select Apply. From the Settings menu in the cloud shell toolbar, select Go to Classic version. now, let’s create a resource group. Choose your desired names for your resource group and your nearest location or location you desire, some locations might not provision key vault resource. I am going to use the following command to do that.

az group create --name myResourceGroup --location eastus

Many of the commands require unique names and use the same parameters. Creating some variables will reduce the changes needed to the commands that create resources. Run the following commands to create the needed variables. Replace myResourceGroup with the name you're using for this lab. If you changed the location in the previous step, make the same change in the location variable.

resourceGroup=myResourceGroup
location=eastus
appConfigName=appconfigname$RANDOM

Run the following command to get the name of App Configuration resource. Record the name, you need it later in the exercise.

echo $appConfigName

Run the following command to ensure the Microsoft.AppConfiguration provider is registered for your subscription.

az provider register --namespace Microsoft.AppConfiguration

It can take a few minutes for the registration to complete. Run the following command to check the status of the registration. Proceed to the next step when the results return Registered.

az provider show --namespace Microsoft.AppConfiguration --query "registrationState"

Run the following command to create an Azure App Configuration resource. This can take a few minutes to run.

az appconfig create --location $location \
    --name $appConfigName \
    --resource-group $resourceGroup
    --sku Free

If there is an issue creating the AppConfig Resource due to quota restrictions using the Free SKU value, use Developer Instead. Free sku did not work for me. It created Standard instead. Developer did not exist. So Microsoft documentation is stating wrong here.

Assign a role to your Microsoft Entra user name

To retrieve configuration information, you need to assign your Microsoft Entra user to the App Configuration Data Reader role.

Run the following command to retrieve the userPrincipalName from your account. This represents who the role will be assigned to.

userPrincipal=$(az rest --method GET --url https://graph.microsoft.com/v1.0/me \
    --headers 'Content-Type=application/json' \
    --query userPrincipalName --output tsv)

Run the following command to retrieve the resource ID of your App Configuration service. The resource ID sets the scope for the role assignment.

resourceID=$(az appconfig show --resource-group $resourceGroup \
    --name $appConfigName --query id --output tsv)

Run the following command to create and assign the App Configuration Data Reader role.

az role assignment create --assignee $userPrincipal \
    --role "App Configuration Data Reader" \
    --scope $resourceID

Next, add a placeholder connection string to App Configuration.

Add configuration information with Azure CLI

In Azure App Configuration, a key like Dev:conStr is a hierarchical, or namespaced key. The colon (:) acts as a delimiter that creates a logical hierarchy, where:

  • Dev represents the namespace or environment prefix (indicating this configuration is for the Development environment)

  • conStr represents the configuration name

    This hierarchical structure allows you to organize configuration settings by environment, feature, or application component, making it easier to manage and retrieve related settings.

Run the following command to store the placeholder connection string.

az appconfig kv set --name $appConfigName \
    --key Dev:conStr \
    --value connectionString \
    --yes

This command returns some JSON. The last line contains the value in plain text.

"value": "connectionString"

Create a .NET console app to retrieve configuration information

Now that the needed resources are deployed to Azure the next step is to set up the console application. The following steps are performed in the cloud shell.

Run the following commands to create a directory to contain the project and change into the project directory.

mkdir appconfig
cd appconfig

Create the .NET console application.

dotnet new console

Run the following commands to add the Azure.Identity and Microsoft.Extensions.Configuration.AzureAppConfiguration packages to the project.

dotnet add package Azure.Identity
dotnet add package Microsoft.Extensions.Configuration.AzureAppConfiguration

Add the code for the project

Run the following command in the cloud shell to begin editing the application.

code Program.cs

Replace any existing contents with the following code. Be sure to replace YOUR_APP_CONFIGURATION_NAME with the name you recorded earlier, and read through the comments in the code.

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
using Azure.Identity;

// Set the Azure App Configuration endpoint, replace YOUR_APP_CONFIGURATION_NAME
// with the name of your actual App Configuration service

string endpoint = "https://YOUR_APP_CONFIGURATION_NAME.azconfig.io"; 

// Configure which authentication methods to use
// DefaultAzureCredential tries multiple auth methods automatically
DefaultAzureCredentialOptions credentialOptions = new()
{
    ExcludeEnvironmentCredential = true,
    ExcludeManagedIdentityCredential = true
};

// Create a configuration builder to combine multiple config sources
var builder = new ConfigurationBuilder();

// Add Azure App Configuration as a source
// This connects to Azure and loads configuration values
builder.AddAzureAppConfiguration(options =>
{

    options.Connect(new Uri(endpoint), new DefaultAzureCredential(credentialOptions));
});

// Build the final configuration object
try
{
    var config = builder.Build();

    // Retrieve a configuration value by key name
    Console.WriteLine(config["Dev:conStr"]);
}
catch (Exception ex)
{
    Console.WriteLine($"Error connecting to Azure App Configuration: {ex.Message}");
}

Press ctrl+s to save the file, then ctrl+q to exit the editor.

This program connects to Azure App Configuration using .NET 10 and retrieves the value of a specified configuration key. It demonstrates secure access to cloud-based configuration settings with minimal code.

Sign into Azure and run the app

In the cloud shell, enter the following command to sign into Azure.

az login

You must sign into Azure - even though the cloud shell session is already authenticated.

Run the following command to start the console app. The app will display the connectionString value you assigned to the Dev:conStr setting earlier in the lab:

dotnet run

The app will display the connectionString value you assigned to the Dev:conStr setting earlier in the lab.

I have run from the vscode on my machine.

Clean up resources

Now that you finished the lab, you should delete the cloud resources you created to avoid unnecessary resource usage. Delete the resource group.

Conclusion

In this article, we have explored

  • Create an Azure App Configuration resource

  • Store connection string configuration information

  • Create a .NET console app to retrieve the configuration information

  • Clean up resources

Reference

https://microsoftlearning.github.io/mslearn-azure-developer/instructions/azure-secure-solutions/02-app-config-retrieve.html