Skip to content

Commit f1864a5

Browse files
Merge pull request #291239 from maud-lv/ml-targetingfilter-aspnet-core
Add Entra ID to howto-targetingfilter-aspnet-core.md
2 parents 3dd26b6 + 2178f6a commit f1864a5

File tree

1 file changed

+77
-12
lines changed

1 file changed

+77
-12
lines changed

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

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ ms.devlang: csharp
77
author: zhiyuanliang-ms
88
ms.author: zhiyuanliang
99
ms.topic: how-to
10-
ms.date: 03/26/2024
10+
ms.date: 12/02/2024
1111
---
1212

1313
# Roll out features to targeted audiences in an ASP.NET Core application
1414

15-
In this guide, you'll use the targeting filter to roll out a feature to targeted audience for your ASP.NET Core application. For more information about the targeting filter, see [Roll out features to targeted audiences](./howto-targetingfilter.md).
15+
In this guide, you'll use the targeting filter to roll out a feature to targeted audiences for your ASP.NET Core application. For more information about the targeting filter, see [Roll out features to targeted audiences](./howto-targetingfilter.md).
1616

1717
## Prerequisites
1818

@@ -23,30 +23,92 @@ In this guide, you'll use the targeting filter to roll out a feature to targeted
2323

2424
## Create a web application with a feature flag
2525

26-
In this section, you will create a web application that allows users to sign in and use the *Beta* feature flag you created before.
26+
In this section, you create a web application that allows users to sign in and use the *Beta* feature flag you created before.
2727

2828
1. Create a web application that authenticates against a local database using the following command.
2929

3030
```dotnetcli
3131
dotnet new webapp --auth Individual -o TestFeatureFlags
3232
```
3333

34-
1. Add references to the following NuGet packages.
34+
1. Navigate to the newly created *TestFeatureFlags* directory and add references to the following NuGet packages.
35+
36+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
37+
38+
```dotnetcli
39+
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore
40+
dotnet add package Microsoft.FeatureManagement.AspNetCore
41+
dotnet add package Azure.Identity
42+
```
43+
44+
### [Connection string](#tab/connection-string)
3545
3646
```dotnetcli
3747
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore
3848
dotnet add package Microsoft.FeatureManagement.AspNetCore
3949
```
50+
---
51+
52+
1. Create a user secret for the application by running the following commands.
53+
54+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
55+
56+
The command uses [Secret Manager](/aspnet/core/security/app-secrets) to store a secret named `Endpoints:AppConfiguration`, which stores the endpoint for your App Configuration store. Replace the `<your-App-Configuration-endpoint>` placeholder with your App Configuration store's endpoint. You can find the endpoint in your App Configuration store's **Overview** blade in the Azure portal.
57+
58+
```dotnetcli
59+
dotnet user-secrets init
60+
dotnet user-secrets set Endpoints:AppConfiguration "<your-App-Configuration-endpoint>"
61+
```
4062
41-
1. Store the connection string for your App Configuration store.
63+
### [Connection string](#tab/connection-string)
64+
65+
The command uses [Secret Manager](/aspnet/core/security/app-secrets) to store a secret named `ConnectionStrings:AppConfiguration`, which stores the connection string for your App Configuration store. Replace the `<your-App-Configuration-connection-string>` placeholder with your App Configuration store's read-only connection string. You can find the connection string in your App Configuration store's **Access settings** in the Azure portal.
4266
4367
```dotnetcli
4468
dotnet user-secrets init
45-
dotnet user-secrets set ConnectionStrings:AppConfig "<your_connection_string>"
69+
dotnet user-secrets set ConnectionStrings:AppConfiguration "<your-App-Configuration-connection-string>"
4670
```
71+
---
4772
4873
1. Add Azure App Configuration and feature management to your application.
4974
75+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
76+
77+
1. You use the `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign your credential the **App Configuration Data Reader** role. Be sure to allow sufficient time for the permission to propagate before running your application.
78+
79+
1. Update the *Program.cs* file with the following code.
80+
81+
``` C#
82+
// Existing code in Program.cs
83+
// ... ...
84+
85+
using Azure.Identity;
86+
87+
var builder = WebApplication.CreateBuilder(args);
88+
89+
// Retrieve the endpoint
90+
string endpoint = builder.Configuration.GetValue<string>("Endpoints:AppConfiguration")
91+
?? throw new InvalidOperationException("The setting `Endpoints:AppConfiguration` was not found.");
92+
93+
// Connect to Azure App Configuration and load all feature flags with no label
94+
builder.Configuration.AddAzureAppConfiguration(options =>
95+
{
96+
options.Connect(new Uri(endpoint), new DefaultAzureCredential())
97+
.UseFeatureFlags();
98+
});
99+
100+
// Add Azure App Configuration middleware to the container of services
101+
builder.Services.AddAzureAppConfiguration();
102+
103+
// Add feature management to the container of services
104+
builder.Services.AddFeatureManagement();
105+
106+
// The rest of existing code in Program.cs
107+
// ... ...
108+
```
109+
110+
### [Connection string](#tab/connection-string)
111+
50112
Update the *Program.cs* file with the following code.
51113
52114
``` C#
@@ -55,14 +117,15 @@ In this section, you will create a web application that allows users to sign in
55117
56118
var builder = WebApplication.CreateBuilder(args);
57119
58-
// Retrieve the App Config connection string
59-
string AppConfigConnectionString = builder.Configuration.GetConnectionString("AppConfig") ?? throw new InvalidOperationException("Connection string 'AppConfig' not found."); ;
120+
// Retrieve the connection string
121+
string connectionString = builder.Configuration.GetConnectionString("AppConfiguration")
122+
?? throw new InvalidOperationException("The connection string 'AppConfiguration' was not found.");
60123
61-
// Load feature flag configuration from Azure App Configuration
124+
// Connect to Azure App Configuration and load all feature flags with no label
62125
builder.Configuration.AddAzureAppConfiguration(options =>
63126
{
64-
options.Connect(AppConfigConnectionString);
65-
options.UseFeatureFlags();
127+
options.Connect(connectionString)
128+
.UseFeatureFlags();
66129
});
67130
68131
// Add Azure App Configuration middleware to the container of services
@@ -75,6 +138,8 @@ In this section, you will create a web application that allows users to sign in
75138
// ... ...
76139
```
77140
141+
---
142+
78143
1. Enable configuration and feature flag refresh from Azure App Configuration with the App Configuration middleware.
79144
80145
Update Program.cs withe the following code.
@@ -104,7 +169,7 @@ In this section, you will create a web application that allows users to sign in
104169
<h1>This is the beta website.</h1>
105170
```
106171
107-
1. Open *Beta.cshtml.cs*, and add `FeatureGate` attribute to the `BetaModel` class.
172+
1. Open *Beta.cshtml.cs*, and add the `FeatureGate` attribute to the `BetaModel` class.
108173
109174
``` C#
110175
using Microsoft.AspNetCore.Mvc.RazorPages;

0 commit comments

Comments
 (0)