-
Notifications
You must be signed in to change notification settings - Fork 69
Set current principal if available #78
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 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
35d2980
Set current principal if available
twsouthwick 6c1b460
Add middleware to restrict concurrency
twsouthwick feb60db
log warnings
twsouthwick 9459bae
create new scheduler for each request
twsouthwick 85b9b88
Merge branch 'tasou/single-thread' into tasou/current-principal
twsouthwick 3273814
Merge remote-tracking branch 'origin/main' into tasou/current-principal
twsouthwick fc1c9ce
clean up
twsouthwick b6c22b4
add checks
twsouthwick 60eb3ea
separate
twsouthwick 688735a
add single threaded to attribute
twsouthwick 5153d7b
Simplify warning
twsouthwick 7a3a5b8
update guidance
twsouthwick 47913a9
Remove duplicate middleware call
twsouthwick 0012888
change log to only trace
twsouthwick 4d00701
add attribute info to docs
twsouthwick 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 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
45 changes: 45 additions & 0 deletions
45
src/Microsoft.AspNetCore.SystemWebAdapters/Adapters/CurrentPrincipalMiddleware.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,45 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.AspNetCore.SystemWebAdapters; | ||
|
|
||
| internal partial class CurrentPrincipalMiddleware | ||
| { | ||
| [LoggerMessage(0, LogLevel.Trace, "Thread.CurrentPrincipal has been set with the current user")] | ||
| private partial void LogCurrentPrincipalUsage(); | ||
|
|
||
| private readonly RequestDelegate _next; | ||
| private readonly ILogger<CurrentPrincipalMiddleware> _logger; | ||
|
|
||
| public CurrentPrincipalMiddleware(RequestDelegate next, ILogger<CurrentPrincipalMiddleware> logger) | ||
| { | ||
| _next = next; | ||
| _logger = logger; | ||
| } | ||
|
|
||
| public Task InvokeAsync(HttpContext context) | ||
| => context.GetEndpoint()?.Metadata.GetMetadata<ISetThreadCurrentPrincipal>() is { IsEnabled: true } ? SetUserAsync(context) : _next(context); | ||
|
|
||
| private async Task SetUserAsync(HttpContext context) | ||
| { | ||
| LogCurrentPrincipalUsage(); | ||
|
|
||
| var current = Thread.CurrentPrincipal; | ||
|
|
||
| try | ||
| { | ||
| Thread.CurrentPrincipal = context.User; | ||
|
|
||
| await _next(context); | ||
| } | ||
| finally | ||
| { | ||
| Thread.CurrentPrincipal = current; | ||
| } | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/Microsoft.AspNetCore.SystemWebAdapters/Adapters/ISetThreadCurrentPrincipal.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,9 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.AspNetCore.SystemWebAdapters; | ||
|
|
||
| public interface ISetThreadCurrentPrincipal | ||
| { | ||
| bool IsEnabled { get; } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/Microsoft.AspNetCore.SystemWebAdapters/Adapters/ISingleThreadedRequestMetadata.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,9 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.AspNetCore.SystemWebAdapters; | ||
|
|
||
| public interface ISingleThreadedRequestMetadata | ||
| { | ||
| bool IsEnabled { get; } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/Microsoft.AspNetCore.SystemWebAdapters/Adapters/SetThreadCurrentPrincipalAttribute.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,12 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.AspNetCore.SystemWebAdapters; | ||
|
|
||
| [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] | ||
| public sealed class SetThreadCurrentPrincipalAttribute : Attribute, ISetThreadCurrentPrincipal, ISingleThreadedRequestMetadata | ||
| { | ||
| public bool IsEnabled { get; set; } = true; | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/Microsoft.AspNetCore.SystemWebAdapters/Adapters/SingleThreadedRequestAttribute.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,12 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.AspNetCore.SystemWebAdapters; | ||
|
|
||
| [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] | ||
| public sealed class SingleThreadedRequestAttribute : Attribute, ISingleThreadedRequestMetadata | ||
| { | ||
| public bool IsEnabled { get; set; } = true; | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/Microsoft.AspNetCore.SystemWebAdapters/Adapters/SingleThreadedRequestMiddleware.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,26 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Http; | ||
|
|
||
| namespace Microsoft.AspNetCore.SystemWebAdapters; | ||
|
|
||
| internal class SingleThreadedRequestMiddleware | ||
| { | ||
| private readonly RequestDelegate _next; | ||
|
|
||
| public SingleThreadedRequestMiddleware(RequestDelegate next) => _next = next; | ||
|
|
||
| public Task InvokeAsync(HttpContextCore context) | ||
| => context.GetEndpoint()?.Metadata.GetMetadata<ISingleThreadedRequestMetadata>() is { IsEnabled: true } | ||
| ? EnsureSingleThreaded(context) | ||
| : _next(context); | ||
|
|
||
| private Task EnsureSingleThreaded(HttpContextCore context) | ||
| { | ||
| var schedule = new ConcurrentExclusiveSchedulerPair(TaskScheduler.Default, 1); | ||
|
|
||
| return Task.Factory.StartNew(() => _next(context), context.RequestAborted, TaskCreationOptions.DenyChildAttach, schedule.ExclusiveScheduler).Unwrap(); | ||
| } | ||
| } |
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.