Setting Thread.CurrentPrincipal with async/await

前端 未结 4 661
礼貌的吻别
礼貌的吻别 2021-01-04 14:14

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

4条回答
  •  逝去的感伤
    2021-01-04 14:26

    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.

提交回复
热议问题