-
Notifications
You must be signed in to change notification settings - Fork 120
Adds Default Targeting Accessor and extension method WithTargeting #466
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
30fc53c
Adds default targeting accessor and extension method WithTargeting va…
rossgrambo 6142f3e
Updates accessor to internal
rossgrambo 04f066d
Forced query evaluation
rossgrambo 1351948
Updated description of .WithTargeting extension
rossgrambo 8640883
Merge branch 'main' into rossgrambo-default-target-accessor
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
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 was deleted.
Oops, something went wrong.
66 changes: 0 additions & 66 deletions
66
examples/FeatureFlagDemo/HttpContextTargetingContextAccessor.cs
This file was deleted.
Oops, something went wrong.
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
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
// 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 Microsoft.FeatureManagement | ||
{ | ||
/// <summary> | ||
/// Provides a default implementation of <see cref="ITargetingContextAccessor"/> that creates <see cref="TargetingContext"/> using info from the current HTTP request. | ||
/// </summary> | ||
internal sealed class DefaultHttpTargetingContextAccessor : ITargetingContextAccessor | ||
{ | ||
/// <summary> | ||
/// The key used to store and retrieve the <see cref="TargetingContext"/> from the <see cref="HttpContext"/> items. | ||
/// </summary> | ||
private static object _cacheKey = new object(); | ||
|
||
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(_cacheKey, 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; | ||
|
||
// | ||
// Treat claims of type Role as groups | ||
IEnumerable<string> groups = httpContext.User.Claims | ||
.Where(c => c.Type == ClaimTypes.Role) | ||
.Select(c => c.Value) | ||
.ToList(); | ||
|
||
TargetingContext targetingContext = new TargetingContext | ||
{ | ||
UserId = userId, | ||
Groups = groups | ||
}; | ||
|
||
// | ||
// Cache for subsequent lookup | ||
httpContext.Items[_cacheKey] = targetingContext; | ||
|
||
return new ValueTask<TargetingContext>(targetingContext); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we call
AddHttpContextAccessor
in this method since our default targeting accessor depends on it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a great question. I originally thought no- so we're in parity with what we have today. However it would remove another hurdle to get started so I think it's a good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to decouple that.
By the way, is this method name the one that we discussed so many options and couldn't settle on one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, let's keep it decoupled. Could always do that in a separate PR anyway.
Yes it is. The last design review it was discussed up was the caching meeting last month. We lightly aligned on
.WithTelemetry<DefaultTargetingContextAccessor>()
, but some smaller discussion afterwards lead to.WithTelemetry()
so we didn't have to makeDefaultTargetingContextAccessor
public.Although I'm having trouble finding a paper trail to confirm that we did commit to this.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zhenlan do you have thoughts on the
.WithTelemetry()
WithTargeting()
over.WithTargeting<DefaultTargetingContextAccessor>()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this would have been referring to
WithTargeting
, notWithTelemetry
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would a blazor user take a dependency on this package and see this method? My thought is that if we go with WithTargeting, any application that would depend on this package should most likely use this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My fault- yes
.WithTargeting
.Blazor Webassembly is just a client side thing- so it would work fine with an ASP.NET backend requiring this.
Blazor Server would not work as expected- and I wouldn't expect the dev to use the ASP.NET package. Although- Blazor Server are built on top of ASP.NET Core so it's not unreasonable a developer would try this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add it by default if 1) it will not break any existing applications and 2) users can easily override it if they want to use their own targeting context accessor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, having it with just
WithTargeting
sounds reasonable to me.