public static async Task Main(string[] args)
        {
            //
            // Setup configuration
            IConfiguration configuration = new ConfigurationBuilder()
                                           .AddJsonFile("appsettings.json")
                                           .Build();

            //
            // Setup application services + feature management
            IServiceCollection services = new ServiceCollection();

            services.AddSingleton(configuration)
            .AddFeatureManagement()
            .AddFeatureFilter <ContextualTargetingFilter>();

            IUserRepository userRepository = new InMemoryUserRepository();

            //
            // Get the feature manager from application services
            using (ServiceProvider serviceProvider = services.BuildServiceProvider())
            {
                IFeatureManager featureManager = serviceProvider.GetRequiredService <IFeatureManager>();

                //
                // We'll simulate a task to run on behalf of each known user
                // To do this we enumerate all the users in our user repository
                IEnumerable <string> userIds = InMemoryUserRepository.Users.Select(u => u.Id);

                //
                // Mimic work items in a task-driven console application
                foreach (string userId in userIds)
                {
                    const string FeatureName = "Beta";

                    //
                    // Get user
                    User user = await userRepository.GetUser(userId);

                    //
                    // Check if feature enabled
                    TargetingContext targetingContext = new TargetingContext
                    {
                        UserId = user.Id,
                        Groups = user.Groups
                    };

                    bool enabled = await featureManager.IsEnabledAsync(FeatureName, targetingContext);

                    //
                    // Output results
                    Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} for the user '{userId}'.");
                }
            }
        }
Beispiel #2
0
        public ValueTask <TargetingContext> GetContextAsync()
        {
            var httpContext = _httpContextAccessor.HttpContext;
            var result      = new TargetingContext();

            result.UserId = httpContext.User.Identity.Name;

            if (httpContext.User.HasClaim("Insider", "true"))
            {
                result.Groups = new string[] { "Insiders" };
            }
            return(new ValueTask <TargetingContext>(result));
        }
        public ValueTask <TargetingContext> GetContextAsync()
        {
            HttpContext httpContext = _httpContextAccessor.HttpContext;

            ClaimsPrincipal user = httpContext.User;

            TargetingContext targetingContext = new TargetingContext
            {
                UserId = user.Identity.Name,
                Groups = GetGroupsFromClaims(user)
            };

            return(new ValueTask <TargetingContext>(targetingContext));
        }
        public async Task <bool> EvaluateAsync(FeatureFilterEvaluationContext context)
        {
            var identityUser = await _identityUserProvider.GetRandomIdentityUserAsync();

            var targetingContext = new TargetingContext
            {
                UserId = identityUser.Id,
                Groups = identityUser.Groups
            };

            var isEnabled = await _contextualTargetingFilter.EvaluateAsync(context, targetingContext);

            if (!isEnabled)
            {
                _logger.LogWarning($"Feature '{Alias}' is not enabled for current identity user '{identityUser}'.");
            }

            return(isEnabled);
        }
        public ValueTask <TargetingContext> GetContextAsync()
        {
            HttpContext httpContext = _httpContextAccessor.HttpContext;

            //
            // Try cache lookup
            if (httpContext.Items.TryGetValue(TargetingContextLookup, out object value))
            {
                return(new ValueTask <TargetingContext>((TargetingContext)value));
            }

            ClaimsPrincipal user = httpContext.User;

            List <string> groups = new List <string>();

            //
            // This application expects groups to be specified in the user's claims
            foreach (Claim claim in user.Claims)
            {
                if (claim.Type == ClaimTypes.GroupName)
                {
                    groups.Add(claim.Value);
                }
            }

            //
            // Build targeting context based off user info
            TargetingContext targetingContext = new TargetingContext
            {
                UserId = user.Identity.Name,
                Groups = groups
            };

            //
            // Cache for subsequent lookup
            httpContext.Items[TargetingContextLookup] = targetingContext;

            return(new ValueTask <TargetingContext>(targetingContext));
        }
Beispiel #6
0
        public ValueTask <TargetingContext> GetContextAsync()
        {
            HttpContext httpContext = _httpContextAccessor.HttpContext;

            if (httpContext.Items.TryGetValue(TargetingContextLookup, out object value))
            {
                return(new ValueTask <TargetingContext>((TargetingContext)value));
            }
            List <string> groups = new List <string>();

            if (httpContext.User.Identity.Name != null)
            {
                groups.Add(httpContext.User.Identity.Name.Split("@", StringSplitOptions.None)[1]);
            }
            TargetingContext targetingContext = new TargetingContext
            {
                UserId = httpContext.User.Identity.Name,
                Groups = groups
            };

            httpContext.Items[TargetingContextLookup] = targetingContext;
            return(new ValueTask <TargetingContext>(targetingContext));
        }
Beispiel #7
0
		public override bool IsTarget(TargetingContext context)
		{
			return context.HttpContext.Request.Browser.IsMobileDevice;
		}
		public override bool IsTarget(TargetingContext context)
		{
			return context.HttpContext.User.Identity.IsAuthenticated;
		}
Beispiel #9
0
 public void SetUp()
 {
     radar   = new TargetingRadar(new N2.Configuration.HostSection(), new DetectorBase[] { new Always(), new Never() });
     context = radar.BuildTargetingContext(new Fakes.FakeHttpContext());
 }
Beispiel #10
0
 public override bool IsTarget(TargetingContext context)
 {
     return(isTarget);
 }
 public override bool IsTarget(TargetingContext context)
 {
     return(context.HttpContext.Request.Browser.IsMobileDevice);
 }
Beispiel #12
0
 public override bool IsTarget(TargetingContext context)
 {
     return context.HttpContext.User.Identity.IsAuthenticated
         && security.IsAuthorized(context.HttpContext.User, Permission.Write);
 }
Beispiel #13
0
 public override bool IsTarget(TargetingContext context)
 {
     return(context.HttpContext.User.Identity.IsAuthenticated &&
            security.IsAuthorized(context.HttpContext.User, Permission.Write));
 }
Beispiel #14
0
 public override bool IsTarget(TargetingContext context)
 {
     throw new NotImplementedException();
 }
Beispiel #15
0
 public override bool IsTarget(TargetingContext context)
 {
     return(context.HttpContext.User.Identity.IsAuthenticated);
 }