-
Notifications
You must be signed in to change notification settings - Fork 126
Adds default http targeting context accessor #416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
rossgrambo
wants to merge
9
commits into
preview
from
rossgrambo/default-http-targeting-context-accessor
Closed
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
87cb9e2
Adds default targeting context accessor and aligns caches
rossgrambo e5fe69b
Adding newline and adjusted namespace
rossgrambo 387f648
Update src/Microsoft.FeatureManagement.AspNetCore/DefaultHttpTargetin…
rossgrambo 7706d4d
Merge branch 'preview' into rossgrambo/default-http-targeting-context…
rossgrambo 622095b
Adjusts to new design
rossgrambo 01ca93a
Update src/Microsoft.FeatureManagement.AspNetCore/DefaultHttpTargetin…
rossgrambo f419784
Removed unused import
rossgrambo 18a4492
Merge branch 'rossgrambo/default-http-targeting-context-accessor' of …
rossgrambo abc8ebf
Avoided exeption in HttpContext.Items read
rossgrambo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/Microsoft.FeatureManagement.AspNetCore/DefaultHttpTargetingContextAccessor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
| // | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.FeatureManagement.FeatureFilters; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Security.Claims; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace FeatureFlagDemo | ||
| { | ||
| /// <summary> | ||
| /// Provides a default implementation of <see cref="ITargetingContextAccessor"/> that creates <see cref="TargetingContext"/> using info from the current HTTP request. | ||
| /// </summary> | ||
| public class DefaultHttpTargetingContextAccessor : ITargetingContextAccessor | ||
|
rossgrambo marked this conversation as resolved.
Outdated
|
||
| { | ||
| private const string TargetingContextLookup = "FeatureManagement.TargetingContext"; | ||
| private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
|
||
| /// <summary> | ||
| /// Creates an instance of the DefaultHttpTargetingContextAccessor | ||
| /// </summary> | ||
| public DefaultHttpTargetingContextAccessor(IHttpContextAccessor httpContextAccessor) | ||
| { | ||
| _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets <see cref="TargetingContext"/> from the current HTTP request. | ||
| /// </summary> | ||
| 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); | ||
| } | ||
|
|
||
| // | ||
| // Treat user identity name as user id | ||
| ClaimsPrincipal user = httpContext.User; | ||
| string userId = user?.Identity?.Name; | ||
|
rossgrambo marked this conversation as resolved.
|
||
|
|
||
| // | ||
| // Treat claims of type Role as groups | ||
| IEnumerable<string> groups = httpContext.User.Claims | ||
| .Where(c => c.Type == ClaimTypes.Role) | ||
| .Select(c => c.Value); | ||
|
|
||
| TargetingContext targetingContext = new TargetingContext | ||
| { | ||
| UserId = userId, | ||
| Groups = groups | ||
| }; | ||
|
|
||
| // | ||
| // Cache for subsequent lookup | ||
| httpContext.Items[TargetingContextLookup] = targetingContext; | ||
|
|
||
| return new ValueTask<TargetingContext>(targetingContext); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ public class TargetingHttpContextMiddleware | |
| private readonly RequestDelegate _next; | ||
| private readonly ILogger _logger; | ||
|
|
||
| private const string TargetingIdKey = $"Microsoft.FeatureManagement.TargetingId"; | ||
| private const string TargetingContextLookup = "FeatureManagement.TargetingContext"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason why we removed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not that I can recall... I'll add it back. |
||
|
|
||
| /// <summary> | ||
| /// Creates an instance of the TargetingHttpContextMiddleware | ||
|
|
@@ -48,9 +48,9 @@ public async Task InvokeAsync(HttpContext context, ITargetingContextAccessor tar | |
|
|
||
| TargetingContext targetingContext = await targetingContextAccessor.GetContextAsync().ConfigureAwait(false); | ||
|
|
||
| if (targetingContext != null) | ||
| if (targetingContext != null && !context.Items.ContainsKey(TargetingContextLookup)) | ||
| { | ||
| context.Items[TargetingIdKey] = targetingContext.UserId; | ||
| context.Items[TargetingContextLookup] = targetingContext; | ||
| } | ||
| else | ||
| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.