While feature flags allow us to turn a feature on/off in real-time without any code changes. Feature filters go one level deep on the feature flags.
Feature filters allow us to filter/limit a feature to a specific group. This allows us to control who has access to the features.
Ex: We can limit the number of people who can see the feature or we can show the feature when the temperature is too hot. You can pretty much do like an if-this-then-that kind of thing with it.
There are three default feature filters available in ASP.NET Core. However, we can create custom feature filters.
In this post, we will see how to create a new custom feature filter with FeatureManagement
package.
Takeaways
1. What are custom feature flags and uses.
2. By the end of this article, you should be able to create a custom feature filter and configure the necessary steps required to make it work.
If you are unaware of what feature management or feature flags are you should first consider reading our comprehensive guide to feature management in asp.net core.
A prior understanding of feature management will make it easy to understand custom feature filters in asp.net core.
Let’s say we want to show a secret feature to only specific people in the organization.
For this article, I’ll create a new asp.net core application with asp.net core identity and create a couple of users. I’m not going to show you how to create users, asp.net core identity, or a new application as it is out of the scope of this article.
Before we dive in and write code to create a custom feature filter. Here is what we will be doing in this article.
Unlike the standard boolean feature flag which is set to either true/false, the custom feature flag is configured differently.
Here is how we configure our custom user feature filter
"FeatureManagement": { "SecretFeature": { "EnabledFor": [ { "Name": "UserFeature", "Parameters": { "Email": [ "karthik@coderethinked.com" ] } } ] } }
Notice how the EnabledFor
section is declared in the above appsettings.json
file. We created an array of objects for EnabledFor
property.
We can have multiple filters for the same feature flag item. If we declare multiple objects in the EnabledFor
array they all would work like an OR filter for the feature flag.
Every object within the array of items in EnabledFor
should have these two properties
Assuming you have prior knowledge of how to install the Microsoft.FeatureManagement
package and setting it up a feature flag. If you don’t, please check this article.
Let’s create a custom feature filter for the user filter.
To create a custom filter we have to implement the IFeatureFilter
interface in our UserFeatureFilter
class.
The IFeatureFilter
interface has EvaluateAsync
method which has to be implemented and it returns a boolean. The result of the method serves as the value of the feature flag for the current request.
Here is our UserFeatureFilter
class.
public class UserFeatureFilter : IFeatureFilter { private readonly IHttpContextAccessor _httpContextAccessor; public UserFeatureFilter(IHttpContextAccessor accessor) { _httpContextAccessor = accessor; } public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context) { var settings = context.Parameters.Get<UserFilterSettings>(); var userIdentity = _httpContextAccessor.HttpContext?.User?.Identity; return Task.FromResult(settings.Email.Contains(userIdentity?.Name)); } }
And here is our UserFilterSettings class
public class UserFilterSettings { public string[] Email { get; set; } }
We are getting the parameters (Email) that are set for the filter in the appsettings.json
file and we are matching the user identity name within the email array.
To know the user name from the HttpContext, we injected IHttpContextAccessor
as a dependency to our filter class and the interface has to be registered in the startup.cs file.
IHttpContextAccessor
should be used with caution. Our HttpContext on the IHttpContextAccessor
instance may go null if there is no HttpContext.
Let’s register our custom feature filter we created in the startup class and also the IHttpContextAccessor.
Within the ConfigureServices method, we register them like this:
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddFeatureManagement().AddFeatureFilter<UserFeatureFilter>();
And that’s it!
That’s it! Thanks for reading.
Karthik is a passionate Full Stack developer working primarily on .NET Core, microservices, distributed systems, VUE and JavaScript. He also loves NBA basketball so you might find some NBA examples in his posts and he owns this blog.
In this post, we’ll see how to test gRPC Server applications using different clients. And… Read More
In this post, we'll create a new gRPC project in ASP.NET Core and see what's… Read More
In this blog post, we’ll see how to run dotnet core projects without opening visual… Read More
Programmatically evaluating policies is useful when we want to provide access or hide some data… Read More
We saw how we could set up policy-based authorization in our previous article. In this… Read More
What is policy-based authorization and how to set up policy-based authorization with handlers and policies… Read More
This website uses cookies.