Skip to content

Commit 075be5a

Browse files
authored
Merge pull request #267642 from zhiyuanliang-ms/zhiyuanliang/update-targeting-usage
Azure App Configuration - Update TargetingFilter tutorial
2 parents b487ed5 + 94444c0 commit 075be5a

File tree

1 file changed

+126
-48
lines changed

1 file changed

+126
-48
lines changed

articles/azure-app-configuration/howto-targetingfilter-aspnet-core.md

Lines changed: 126 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,143 @@ titleSuffix: Azure App Configuration
44
description: Learn how to enable staged rollout of features for targeted audiences.
55
ms.service: azure-app-configuration
66
ms.devlang: csharp
7-
author: maud-lv
8-
ms.author: malev
9-
ms.topic: conceptual
10-
ms.date: 02/16/2024
7+
author: zhiyuanliang
8+
ms.author: zhiyuanliang
9+
ms.topic: how-to
10+
ms.date: 03/05/2024
1111
---
1212

1313
# Enable staged rollout of features for targeted audiences
1414

15-
Feature flags allow you to dynamically activate or deactivate functionality in your application. Feature filters determine the state of a feature flag each time it's evaluated. The `Microsoft.FeatureManagement` library includes `TargetingFilter`, which enables a feature flag for a specified list of users and groups, or for a specified percentage of users. `TargetingFilter` is "sticky." This means that once an individual user receives a feature, they'll continue to see that feature on all future requests. You can use `TargetingFilter` to enable a feature for a specific account during a demo, to progressively roll out new features to users in different groups or "rings," and much more.
15+
Targeting is a feature management strategy that enables developers to progressively roll out new features to their user base. The strategy is built on the concept of targeting a set of users known as the target audience. An audience is made up of specific users, groups, and a designated percentage of the entire user base.
16+
17+
- The users can be actual user accounts, but they can also be machines, devices, or any uniquely identifiable entities to which you want to roll out a feature.
18+
19+
- The groups are up to your application to define. For example, when targeting user accounts, you can use Microsoft Entra groups or groups denoting user locations. When targeting machines, you can group them based on rollout stages. Groups can be any common attributes based on which you want to categorize your audience.
1620

1721
In this article, you learn how to roll out a new feature in an ASP.NET Core web application to specified users and groups, using `TargetingFilter` with Azure App Configuration.
1822

1923
## Prerequisites
2024

2125
- Finish the [Quickstart: Add feature flags to an ASP.NET Core app](./quickstart-feature-flag-aspnet-core.md).
22-
- Update the `Microsoft.FeatureManagement.AspNetCore` package to version **2.6.0** or later.
26+
- Update the [`Microsoft.FeatureManagement.AspNetCore`](https://www.nuget.org/packages/Microsoft.FeatureManagement.AspNetCore/) package to version **3.0.0** or later.
2327

24-
## Create a web application with feature flags and authentication
28+
## Create a web application with authentication and feature flags
2529

26-
To roll out features based on users and groups, you need a web application that allows users to sign in.
30+
In this section, you will create a web application that allows users to sign in and use the **Beta** feature flag you created before. Most of the steps are very similar to what you have done in [Quickstart](./quickstart-feature-flag-aspnet-core.md).
2731

2832
1. Create a web application that authenticates against a local database using the following command.
2933

3034
```dotnetcli
3135
dotnet new mvc --auth Individual -o TestFeatureFlags
3236
```
3337

34-
1. Build and run. Then select the **Register** link in the upper right corner to create a new user account. Use an email address of `[email protected]`. On the **Register Confirmation** screen, select **Click here to confirm your account**.
38+
1. Add references to the following NuGet packages.
39+
40+
```dotnetcli
41+
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore
42+
dotnet add package Microsoft.FeatureManagement.AspNetCore
43+
```
44+
45+
1. Store the connection string for your App Configuration store.
46+
47+
```dotnetcli
48+
dotnet user-secrets init
49+
dotnet user-secrets set ConnectionStrings:AppConfig "<your_connection_string>"
50+
```
51+
52+
1. Update *Program.cs* with the following code.
53+
54+
``` C#
55+
// Existing code in Program.cs
56+
// ... ...
57+
58+
var builder = WebApplication.CreateBuilder(args);
59+
60+
// Retrieve the connection string
61+
string connectionString = builder.Configuration.GetConnectionString("AppConfig");
62+
63+
// Load configuration from Azure App Configuration
64+
builder.Configuration.AddAzureAppConfiguration(options =>
65+
{
66+
options.Connect(connectionString);
67+
options.UseFeatureFlags();
68+
});
69+
70+
// Add Azure App Configuration middleware to the container of services
71+
builder.Services.AddAzureAppConfiguration();
72+
73+
// Add feature management to the container of services
74+
builder.Services.AddFeatureManagement();
75+
76+
// The rest of existing code in Program.cs
77+
// ... ...
78+
```
79+
80+
``` C#
81+
// Existing code in Program.cs
82+
// ... ...
83+
84+
var app = builder.Build();
85+
86+
// Use Azure App Configuration middleware for dynamic configuration refresh
87+
app.UseAzureAppConfiguration();
88+
89+
// The rest of existing code in Program.cs
90+
// ... ...
91+
```
92+
93+
1. Add *Beta.cshtml* under the *Views\Home* directory and update it with the following markup.
94+
95+
``` cshtml
96+
@{
97+
ViewData["Title"] = "Beta Page";
98+
}
3599
36-
1. Follow the instructions in the [Quickstart](./quickstart-feature-flag-aspnet-core.md) to add a feature flag to your new web application.
100+
<h1>This is the beta website.</h1>
101+
```
102+
103+
1. Open *HomeController.cs* under the *Controllers* directory and update it with the following code.
104+
105+
``` C#
106+
public IActionResult Beta()
107+
{
108+
return View();
109+
}
110+
```
111+
112+
1. Open *_ViewImports.cshtml*, and register the feature manager Tag Helper using an `@addTagHelper` directive:
113+
114+
``` cshtml
115+
@addTagHelper *, Microsoft.FeatureManagement.AspNetCore
116+
```
117+
118+
1. Open *_Layout.cshtml* in the Views\Shared directory. Insert a new `<feature>` tag in between the *Home* and *Privacy* navbar items.
119+
120+
``` html
121+
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
122+
<ul class="navbar-nav flex-grow-1">
123+
<li class="nav-item">
124+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
125+
</li>
126+
<feature name="Beta">
127+
<li class="nav-item">
128+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Beta">Beta</a>
129+
</li>
130+
</feature>
131+
<li class="nav-item">
132+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
133+
</li>
134+
</ul>
135+
<partial name="_LoginPartial" />
136+
</div>
137+
```
138+
139+
1. Build and run. Then select the **Register** link in the upper right corner to create a new user account. Use an email address of `[email protected]`. On the **Register Confirmation** screen, select **Click here to confirm your account**.
37140
38141
1. Toggle the feature flag in App Configuration. Validate that this action controls the visibility of the **Beta** item on the navigation bar.
39142
40-
## Update the web application code to use TargetingFilter
143+
## Update the web application code to use `TargetingFilter`
41144
42145
At this point, you can use the feature flag to enable or disable the `Beta` feature for all users. To enable the feature flag for some users while disabling it for others, update your code to use `TargetingFilter`. In this example, you use the signed-in user's email address as the user ID, and the domain name portion of the email address as the group. You add the user and group to the `TargetingContext`. The `TargetingFilter` uses this context to determine the state of the feature flag for each request.
43146
@@ -47,7 +150,7 @@ At this point, you can use the feature flag to enable or disable the `Beta` feat
47150
dotnet add package Microsoft.FeatureManagement.AspNetCore
48151
```
49152

50-
1. Add a *TestTargetingContextAccessor.cs* file.
153+
1. Add *ExampleTargetingContextAccessor.cs* file.
51154

52155
```csharp
53156
using Microsoft.AspNetCore.Http;
@@ -58,12 +161,12 @@ At this point, you can use the feature flag to enable or disable the `Beta` feat
58161

59162
namespace TestFeatureFlags
60163
{
61-
public class TestTargetingContextAccessor : ITargetingContextAccessor
164+
public class ExampleTargetingContextAccessor : ITargetingContextAccessor
62165
{
63-
private const string TargetingContextLookup = "TestTargetingContextAccessor.TargetingContext";
166+
private const string TargetingContextLookup = "ExampleTargetingContextAccessor.TargetingContext";
64167
private readonly IHttpContextAccessor _httpContextAccessor;
65168

66-
public TestTargetingContextAccessor(IHttpContextAccessor httpContextAccessor)
169+
public ExampleTargetingContextAccessor(IHttpContextAccessor httpContextAccessor)
67170
{
68171
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
69172
}
@@ -92,48 +195,23 @@ At this point, you can use the feature flag to enable or disable the `Beta` feat
92195
}
93196
```
94197

95-
1. In *Startup.cs*, add a reference to the *Microsoft.FeatureManagement.FeatureFilters* namespace.
198+
1. Open `Program.cs` and add the `ExampleTargetingContextAccessor` created in the earlier step and `TargetingFilter` to the service collection by calling the `WithTargeting` method after the existing line of `AddFeatureManagement`. The `TargetingFilter` will use the `ExampleTargetingContextAccessor` to determine the targeting context every time that the feature flag is evaluated.
96199

97200
```csharp
98-
using Microsoft.FeatureManagement.FeatureFilters;
99-
```
201+
// Existing code in Program.cs
202+
// ... ...
100203
101-
1. Update the *ConfigureServices* method to register `TargetingFilter`, following the call to `AddFeatureManagement()`.
204+
// Add feature management to the container of services
205+
builder.Services.AddFeatureManagement()
206+
.WithTargeting<ExampleTargetingContextAccessor>();
102207

103-
```csharp
104-
services.AddFeatureManagement()
105-
.AddFeatureFilter<TargetingFilter>();
208+
// The rest of existing code in Program.cs
209+
// ... ...
106210
```
107211

108212
> [!NOTE]
109213
> For Blazor applications, see [instructions](./faq.yml#how-to-enable-feature-management-in-blazor-applications-or-as-scoped-services-in--net-applications) for enabling feature management as scoped services.
110214

111-
1. Update the *ConfigureServices* method to add the `TestTargetingContextAccessor` created in the earlier step to the service collection. The *TargetingFilter* uses it to determine the targeting context every time that the feature flag is evaluated.
112-
113-
```csharp
114-
services.AddSingleton<ITargetingContextAccessor, TestTargetingContextAccessor>();
115-
```
116-
117-
The entire *ConfigureServices* method looks like this.
118-
119-
```csharp
120-
public void ConfigureServices(IServiceCollection services)
121-
{
122-
services.AddDbContext<ApplicationDbContext>(options =>
123-
options.UseSqlite(
124-
Configuration.GetConnectionString("DefaultConnection")));
125-
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
126-
.AddEntityFrameworkStores<ApplicationDbContext>();
127-
services.AddControllersWithViews();
128-
services.AddRazorPages();
129-
130-
// Add feature management, targeting filter, and ITargetingContextAccessor to service collection
131-
services.AddFeatureManagement()
132-
.AddFeatureFilter<TargetingFilter>();
133-
services.AddSingleton<ITargetingContextAccessor, TestTargetingContextAccessor>();
134-
}
135-
```
136-
137215
## Update the feature flag to use TargetingFilter
138216

139217
1. In the Azure portal, go to your App Configuration store and select **Feature manager**.

0 commit comments

Comments
 (0)