Below is a simplified version of where I am trying to set Thread.CurrentPrincipal within an async method to a custom UserPrincipal object but the custom object is getting lo
ExecutionContext
, which contains SecurityContext
, which contains CurrentPrincipal
, is pretty-much always flowed across all asynchronous forks. So in your Task.Run()
delegate, you - on a separate thread as you note, get the same CurrentPrincipal
. However, under the hood, you get the context flowed via ExecutionContext.Run(...), which states:
The execution context is returned to its previous state when the method completes.
I find myself in strange territory differing with Stephen Cleary :), but I don't see how SynchronizationContext
has anything to do with this.
Stephen Toub covers most of this in an excellent article here.