Preventing the distribution of Thread.CurrentPrincipal in all application domains

Anyone if you can stop the current IPrincipal flow from spreading across the application domain border? I do not control the IPrincipal assigned to the stream, but I have control over the creation of application domains.

(The reason I want to do this is to prevent a serialization error from occurring if the assembly of the main object is not available in another domain.)

Edit: ExecutionContext.SuppressFlow looks promising, but it doesn't seem to reach the goal. The following "MyIdentity" prints:

 static void Main () { ExecutionContext.SuppressFlow (); Thread.CurrentPrincipal = new GenericPrincipal (new GenericIdentity ("MyIdentity"), "Role".Split ()); AppDomain.CreateDomain ("New domain").DoCallBack (Isolated); } static void Isolated () { Console.WriteLine ("Current principal: " + Thread.CurrentPrincipal.Identity.Name); // MyIdentity } 
+4
source share
2 answers

You did not run the asynchronous method, the target function is executed in the secondary application domain by the same thread. Thus, the principle does not change. It 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 start the same thread with a specific context, 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); 
+5
source

This is similar to what you want:

System.Threading.ExecutionContext

In particular, take a look at the SuppressFlow method.

Chris

+2
source

Source: https://habr.com/ru/post/920421/


All Articles