Preventing Thread.CurrentPrincipal from propagating across application domains

拟墨画扇 提交于 2019-12-05 16:14:22

You didn't run an asynchronous method, the target function is executed in the secondary appdomain by the same thread. So the principal doesn't change. This works:

        var flow = ExecutionContext.SuppressFlow();
        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("MyIdentity"), "Role".Split());
        ThreadPool.QueueUserWorkItem((x) => {
            AppDomain.CreateDomain("New domain").DoCallBack(Isolated);
        });
        flow.Undo();

Or if you just want to run the same thread with a specific context then you can use ExecutionContext.Run():

        var copy = ExecutionContext.Capture();
        Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("MyIdentity"), "Role".Split());
        ExecutionContext.Run(copy, new ContextCallback((x) => {
            AppDomain.CreateDomain("New domain").DoCallBack(Isolated);
        }), null);

This appears to do what you want:

System.Threading.ExecutionContext

Specifically, take a look at the SuppressFlow method.

Chris

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!