File tree Expand file tree Collapse file tree 9 files changed +85
-3
lines changed
CustomFeatureFilter/CustomFeatureFilter Expand file tree Collapse file tree 9 files changed +85
-3
lines changed Original file line number Diff line number Diff line change 1+ using Microsoft . AspNetCore . Http ;
2+ using Microsoft . Extensions . Configuration ;
3+ using Microsoft . FeatureManagement ;
4+ using System ;
5+ using System . Linq ;
6+
7+ namespace CustomFeatureFilter
8+ {
9+ [ FilterAlias ( "Claims" ) ]
10+ public class ClaimsFeatureFilter : IFeatureFilter
11+ {
12+ private readonly IHttpContextAccessor _httpContextAccesor ;
13+
14+ public ClaimsFeatureFilter ( IHttpContextAccessor httpContextAccesor )
15+ {
16+ _httpContextAccesor = httpContextAccesor ?? throw new ArgumentNullException ( nameof ( httpContextAccesor ) ) ;
17+ }
18+
19+ public bool Evaluate ( FeatureFilterEvaluationContext context )
20+ {
21+ var settings = context . Parameters . Get < ClaimsFilterSettings > ( ) ;
22+
23+ var user = _httpContextAccesor . HttpContext . User ;
24+
25+ return settings . RequiredClaims
26+ . All ( claimType => user . HasClaim ( claim => claim . Type == claimType ) ) ;
27+ }
28+ }
29+
30+ }
Original file line number Diff line number Diff line change 1+ namespace CustomFeatureFilter
2+ {
3+ public class ClaimsFilterSettings
4+ {
5+ public string [ ] RequiredClaims { get ; set ; }
6+ }
7+
8+ }
Original file line number Diff line number Diff line change 99
1010 <ItemGroup >
1111 <PackageReference Include =" Microsoft.AspNetCore.App" />
12+ <PackageReference Include =" Microsoft.FeatureManagement.AspNetCore" Version =" 1.0.0-preview-008560001-910" />
1213 <PackageReference Include =" Microsoft.AspNetCore.Razor.Design" Version =" 2.2.0" PrivateAssets =" All" />
1314 </ItemGroup >
1415
Original file line number Diff line number Diff line change 1+ namespace CustomFeatureFilter
2+ {
3+ public static class FeatureFlags
4+ {
5+ public const string Beta = "Beta" ;
6+ public const string NewWelcomeBanner = "NewWelcomeBanner" ;
7+ }
8+ }
Original file line number Diff line number Diff line change 44 ViewData [" Title" ] = " Home page" ;
55}
66
7+ <feature name =" @FeatureFlags.Beta" >
8+ <div class =" alert alert-primary" role =" alert" >
9+ Congratulations - You're in the Beta test!
10+ </div >
11+ </feature >
12+
713<div class =" text-center" >
8- <h1 class =" display-4" >Welcome </h1 >
14+ <h1 class =" display-4" >@Model.WelcomeMessage </h1 >
915 <p >Learn about <a href =" https://docs.microsoft.com/aspnet/core" >building Web apps with ASP.NET Core</a >.</p >
1016</div >
Original file line number Diff line number Diff line change 44using System . Threading . Tasks ;
55using Microsoft . AspNetCore . Mvc ;
66using Microsoft . AspNetCore . Mvc . RazorPages ;
7+ using Microsoft . FeatureManagement ;
78
89namespace CustomFeatureFilter . Pages
910{
1011 public class IndexModel : PageModel
1112 {
12- public void OnGet ( )
13+ private readonly IFeatureManagerSnapshot _featureManager ;
14+
15+ public IndexModel ( IFeatureManagerSnapshot featureManager )
1316 {
17+ _featureManager = featureManager ;
18+ }
1419
20+ public string WelcomeMessage { get ; set ; }
21+
22+ public void OnGet ( )
23+ {
24+ WelcomeMessage = _featureManager . IsEnabled ( FeatureFlags . NewWelcomeBanner )
25+ ? "Welcome to the Beta"
26+ : "Welcome" ;
1527 }
1628 }
1729}
Original file line number Diff line number Diff line change 33@using CustomFeatureFilter .Data
44@namespace CustomFeatureFilter.Pages
55@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
6+ @addTagHelper *, Microsoft.FeatureManagement.AspNetCore
Original file line number Diff line number Diff line change 1313using CustomFeatureFilter . Data ;
1414using Microsoft . Extensions . Configuration ;
1515using Microsoft . Extensions . DependencyInjection ;
16+ using Microsoft . FeatureManagement ;
1617
1718namespace CustomFeatureFilter
1819{
@@ -45,6 +46,8 @@ public void ConfigureServices(IServiceCollection services)
4546
4647 services . AddMvc ( ) . SetCompatibilityVersion ( CompatibilityVersion . Version_2_2 ) ;
4748
49+ services . AddFeatureManagement ( )
50+ . AddFeatureFilter < ClaimsFeatureFilter > ( ) ;
4851 }
4952
5053 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Original file line number Diff line number Diff line change 77 "Default" : " Warning"
88 }
99 },
10- "AllowedHosts" : " *"
10+ "AllowedHosts" : " *" ,
11+ "FeatureManagement" : {
12+ "NewWelcomeBanner" : true ,
13+ "Beta" : {
14+ "EnabledFor" : [
15+ {
16+ "Name" : " Claims" ,
17+ "Parameters" : {
18+ "RequiredClaims" : [ " Internal" ]
19+ }
20+ }
21+ ]
22+ }
23+ }
1124}
You can’t perform that action at this time.
0 commit comments