We have a problem with System.Threading.ExecutionContext and System.Threading.Thread.CurrentPrincipal on .NET Core 2.1.
We want to implement a custom asynchronous point. According to docs.microsoft.com System.Threading.ExecutionContext is the way to go:
The ExecutionContext class provides the functionality for user code to capture and transfer this context across user-defined asynchronous points.
Test:
using System;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main()
{
var principal = new ClaimsPrincipal();
Thread.CurrentPrincipal = principal;
var context = ExecutionContext.Capture();
Thread.CurrentPrincipal = null;
ExecutionContext.Run
(
context,
state =>
{
Console.WriteLine("Thread.CurrentPrincipal: {0}", Thread.CurrentPrincipal);
},
null
);
}
}
Using .NET Framework 4.7.1 I get:
Thread.CurrentPrincipal: System.Security.Claims.ClaimsPrincipal
Wich is the expected behavior, according to the documentation
The ExecutionContext class provides a single container for all information relevant to a logical thread of execution. This includes security context, call context, and synchronization context.
Using .NET Core 2.1 I get:
Thread.CurrentPrincipal:
I tested the behavior with the runtime provided asynchronoues point "await" and "Task.Run". In both cases it worked as expected.
Is this behavioral change intended?
We have a problem with System.Threading.ExecutionContext and System.Threading.Thread.CurrentPrincipal on .NET Core 2.1.
We want to implement a custom asynchronous point. According to docs.microsoft.com System.Threading.ExecutionContext is the way to go:
Test:
Using .NET Framework 4.7.1 I get:
Thread.CurrentPrincipal: System.Security.Claims.ClaimsPrincipal
Wich is the expected behavior, according to the documentation
Using .NET Core 2.1 I get:
Thread.CurrentPrincipal:
I tested the behavior with the runtime provided asynchronoues point "await" and "Task.Run". In both cases it worked as expected.
Is this behavioral change intended?