It is very important to keep secrets and settings out of source code to make sure that they can be changed easily and can be secured. A good way to do that, is to store your secrets and settings in a central service. Azure provides a service like that, and it's called Azure App Configuration.
Azure App Configuration can store secrets and settings for you in a central place, so that your application doesn't have to store them in source code. On top of that, it has the ability to manage feature flags. These are settings that you use in your application to enable or disable features, like a new menu-item that is only visible when the feature flag 'beta' is enabled. This allows you to deploy changes to production, and only make them visible when you choose to.
In this article, we'll take a look at how you can use Azure App Configuration with an ASP.NET Core application and how you can use feature flags.
To follow along with this article, you'll need the following:
Before we create the application that uses settings and features flags, we'll create an App Configuration to store those in.
We'll create the Azure App Configuration in the Azure portal.
The App Configuration will now be created.
(The App Configuration creation blade in the Azure portal)
Once the App Configuration is created, you can use it to store values and configure a feature flag. Let's do that, so that we can use those values in the application that we are going to create.
Now, we have one feature called Beta and it is disabled.
(Add a new feature flag to App Configuration)
Next, we'll add an application setting that we are going to use in the application.
(Add a new key-value pair to App Configuration)
For the next step, we'll need a connection string to connect to the Azure App Configuration.
That's it! We have everything set up for our application.
Now, we are going to use Visual Studio 2019 to create an ASP.NET Core web application and use the Azure App Configuration.
The first thing that we'll do, is to add a configuration value that will enable us to connect to App Configuration.
This saves the connection string to App Configuration in the Secret Manager Tool, which keeps it out of your source code.
(Access the Secret Manager Tool in Visual Studio)
At the time of this writing, ASP.NET Core is not able to talk to Azure App Configuration and use feature flags. We need to add two NuGet packages to help us:
(If you can't find the packages in NuGet, make sure to also search for preview versions)
Now, we need to change some code in our project:
In Program.cs:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var settings = config.Build();
config.AddAzureAppConfiguration(options => {
options.Connect(settings["ConnectionStrings:AppConfig"])
.UseFeatureFlags();
});
})
.UseStartup<Startup>();
This enables us to connect to the Azure App Configuration.
In Startup.cs:
services.AddFeatureManagement();
This will add feature flag management to the application.
First, we will use the application setting that we've added to App Configuration.
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">@Configuration["MyTextValue"]</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
This uses the configuration to get the value of the MyTextValue setting in App Configuration.
Next, we'll implement code to use the feature flag.
public enum MyFeatureFlags
{
Beta
}
This is the feature flag that we will use. It matches the Key name that we entered in the Azure portal.
In the Controllers folder, add BetaController.cs and add the following code to it:
using Microsoft.AspNetCore.Mvc;
using Microsoft.FeatureManagement;
using Microsoft.FeatureManagement.Mvc;
namespace AppConfiguration.Controllers
{
public class BetaController: Controller
{
private readonly IFeatureManager _featureManager;
public BetaController(IFeatureManagerSnapshot featureManager)
{
_featureManager = featureManager;
}
[FeatureGate(MyFeatureFlags.Beta)]
public IActionResult Index()
{
return View();
}
}
}
This controller will be able to serve the Beta page, only when the Beta feature flag is enabled.
In the Views folder, open the _ViewImports.cshtml file and add the following line of code to it:
@addTagHelper *, Microsoft.FeatureManagement.AspNetCore
This taghelper enables us to use feature flags in views.
In the Views\Shared folder, open the _Layout.cshtml file. Replace the navigation bar code (which is the code between the <nav> elements, with the following:
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">AppConfiguration</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
<feature name="Beta">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Beta" asp-action="Index">Beta</a>
</li>
</feature>
</ul>
</div>
</div>
</nav>
This adds an extra menu-item called Beta. It does that only when the Beta feature flag is enabled.
Next, in the Views folder, add a folder called Beta. Finally, add a file, called Index.cshtml to that folder and add the following code to it:
@{
ViewData["Title"] = "Beta Home Page";
}
<h1>
This is the beta website.
</h1>
That is everything we need. Let's run the application to see what happens. It will be able to connect to Azure App Configuration and get the MyTextValue setting, like in the image below.
(The text from the MyTextValue setting is visible)
Now go back to the Azure portal and to the App Configuration. In there, go to the feature management menu and change the Beta feature to enabled. When you restart the ASP.NET Core application, you'll see that there is now a new menu-item called beta, which takes you to the beta page.
(The new beta menu-item is visible)
Storing your application settings and secrets outside of your code is vital for security and management. Azure App Configuration is a great place to store your settings. It also enables you to work in a DevOps way, so that settings and secrets don't have to be known or managed by developers. Also, the feature flag management of Azure App Configuration is very easy to use and provides a lot of value to your application. You can learn more about the feature flag capability.
Create a trial account today and go and check it out!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.