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 = user.Claims.Where(claim => claim.Type == ClaimTypes.GroupName) .Select(claim => claim.Value).ToList(); // // This application expects groups to be specified in the user's claims // // Build targeting context based off user info var targetingContext = new TargetingContext { UserId = user.Identity.Name, Groups = groups }; // // Cache for subsequent lookup httpContext.Items[TargetingContextLookup] = targetingContext; return(new ValueTask <TargetingContext>(targetingContext)); }
/// <summary> /// Performs a targeting evaluation using the current <see cref="TargetingContext"/> to determine if a feature should be enabled. /// </summary> /// <param name="context">The feature evaluation context.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="context"/> is null.</exception> /// <returns>True if the feature is enabled, false otherwise.</returns> public async Task <bool> EvaluateAsync(FeatureFilterEvaluationContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } // // Acquire targeting context via accessor TargetingContext targetingContext = await _contextAccessor.GetContextAsync().ConfigureAwait(false); // // Ensure targeting can be performed if (targetingContext == null) { _logger.LogWarning("No targeting context available for targeting evaluation."); return(false); } // // Utilize contextual filter for targeting evaluation return(await _contextualFilter.EvaluateAsync(context, targetingContext).ConfigureAwait(false)); }