Exemplo n.º 1
0
        private void Thread_Run_UnmanagedCode_SuppressFlow_AfterCapture()
        {
            bool result = false;

            Assert.IsFalse(success, "pre-check");
            ExecutionContext ec  = GetExecutionContextUnmanaged();
            AsyncFlowControl afc = ExecutionContext.SuppressFlow();

            try {
                ExecutionContext.Run(ec, new ContextCallback(Callback), true);
            }
            catch (SecurityException) {
                result = true;
            }
            finally {
                Assert.IsFalse(success, "post-check");
                afc.Undo();
                Assert.IsTrue(result, "Result");
            }
        }
Exemplo n.º 2
0
        private void Thread_Run_UnmanagedCode_SuppressFlow_BeforeCapture()
        {
            bool result = false;

            Assert.IsFalse(success, "pre-check");
            AsyncFlowControl afc = SecurityContext.SuppressFlow();

            try {
                SecurityContext sc = GetSecurityContextUnmanaged();
                SecurityContext.Run(sc, new ContextCallback(Callback), true);
            }
            catch (InvalidOperationException) {
                result = true;
            }
            finally {
                Assert.IsFalse(success, "post-check");
                afc.Undo();
                Assert.IsTrue(result, "Result");
            }
        }
        private static T EscapeExecutionContext <T>(Func <T> callback)
        {
            AsyncFlowControl suppressExecutionContextFlow = ExecutionContext.SuppressFlow();
            T retval2;

            try
            {
                T         retval          = default(T);
                Exception threadException = null;
                Thread    thread          = new Thread(new ThreadStart(delegate
                {
                    try
                    {
                        try
                        {
                            SCryptAlgorithm.SafeSetPrincipal(null);
                            retval = callback();
                        }
                        catch (Exception e)
                        {
                            threadException = e;
                        }
                    }
                    catch
                    {
                    }
                }));
                thread.Start();
                thread.Join();
                if (threadException != null)
                {
                    throw new TargetInvocationException(threadException);
                }
                retval2 = retval;
            }
            finally
            {
                suppressExecutionContextFlow.Undo();
            }
            return(retval2);
        }
Exemplo n.º 4
0
        internal static SecurityContext CaptureSecurityContextNoIdentityFlow()
        {
            SecurityContext securityContext;

            if (!SecurityContext.IsWindowsIdentityFlowSuppressed())
            {
                AsyncFlowControl asyncFlowControl = SecurityContext.SuppressFlowWindowsIdentity();
                try
                {
                    securityContext = SecurityContext.Capture();
                }
                finally
                {
                    asyncFlowControl.Dispose();
                }
                return(securityContext);
            }
            else
            {
                return(SecurityContext.Capture());
            }
        }
Exemplo n.º 5
0
        public void SuppressFlow_Both_Undo()
        {
            Assert.IsFalse(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-1");
            Assert.IsFalse(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-1");

            AsyncFlowControl afc = SecurityContext.SuppressFlowWindowsIdentity();

            Assert.IsFalse(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-2");
            Assert.IsTrue(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-2");

            AsyncFlowControl afc2 = SecurityContext.SuppressFlow();

            Assert.IsTrue(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-2");
            Assert.IsTrue(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-2");
            afc2.Undo();

            // note: afc2 Undo return to the original (not the previous) state
            Assert.IsFalse(SecurityContext.IsFlowSuppressed(), "IsFlowSuppressed-2");
            Assert.IsFalse(SecurityContext.IsWindowsIdentityFlowSuppressed(), "IsWindowsIdentityFlowSuppressed-2");

            // we can't use the first AsyncFlowControl
            afc.Undo();
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            new Thread(() =>
            {
                Console.WriteLine("i-0:" + _localThread.Value);
            }).Start();


            new Thread(() =>
            {
                Console.WriteLine("i-1:" + _localThread.Value);
            }).Start();
            AsyncFlowControl aFC = ExecutionContext.SuppressFlow();

            Console.WriteLine("Is the flow suppressed? " +
                              ExecutionContext.IsFlowSuppressed());
            Console.WriteLine("Restore the flow.");
            aFC.Undo();
            Console.WriteLine("Is the flow suppressed? " +
                              ExecutionContext.IsFlowSuppressed());

            Console.ReadKey();
        }
        public static void AsyncFlowControlTest()
        {
            ThreadTestHelpers.RunTestInBackgroundThread(() =>
            {
                Action<AsyncFlowControl, AsyncFlowControl, bool> verifyEquality =
                    (afc0, afc1, areExpectedToBeEqual) =>
                    {
                        Assert.Equal(areExpectedToBeEqual, afc0.Equals(afc1));
                        Assert.Equal(areExpectedToBeEqual, afc0.Equals((object)afc1));
                        Assert.Equal(areExpectedToBeEqual, afc0 == afc1);
                        Assert.NotEqual(areExpectedToBeEqual, afc0 != afc1);
                    };

                AsyncFlowControl asyncFlowControl0 = ExecutionContext.SuppressFlow();
                ExecutionContext.RestoreFlow();
                AsyncFlowControl asyncFlowControl1 = ExecutionContext.SuppressFlow();
                ExecutionContext.RestoreFlow();
                verifyEquality(asyncFlowControl0, asyncFlowControl1, true);
                verifyEquality(asyncFlowControl1, asyncFlowControl0, true);

                var asyncLocal = new AsyncLocal<int>();
                asyncLocal.Value = 1;
                asyncFlowControl1 = ExecutionContext.SuppressFlow();
                ExecutionContext.RestoreFlow();
                verifyEquality(asyncFlowControl0, asyncFlowControl1, true);
                verifyEquality(asyncFlowControl1, asyncFlowControl0, true);

                asyncFlowControl1 = new AsyncFlowControl();
                verifyEquality(asyncFlowControl0, asyncFlowControl1, false);
                verifyEquality(asyncFlowControl1, asyncFlowControl0, false);

                ThreadTestHelpers.RunTestInBackgroundThread(() => asyncFlowControl1 = ExecutionContext.SuppressFlow());
                verifyEquality(asyncFlowControl0, asyncFlowControl1, false);
                verifyEquality(asyncFlowControl1, asyncFlowControl0, false);
            });
        }
Exemplo n.º 8
0
        public static void AsyncFlowControlTest()
        {
            ThreadTestHelpers.RunTestInBackgroundThread(() =>
            {
                Action <AsyncFlowControl, AsyncFlowControl, bool> verifyEquality =
                    (afc0, afc1, areExpectedToBeEqual) =>
                {
                    Assert.Equal(areExpectedToBeEqual, afc0.Equals(afc1));
                    Assert.Equal(areExpectedToBeEqual, afc0.Equals((object)afc1));
                    Assert.Equal(areExpectedToBeEqual, afc0 == afc1);
                    Assert.NotEqual(areExpectedToBeEqual, afc0 != afc1);
                };

                AsyncFlowControl asyncFlowControl0 = ExecutionContext.SuppressFlow();
                ExecutionContext.RestoreFlow();
                AsyncFlowControl asyncFlowControl1 = ExecutionContext.SuppressFlow();
                ExecutionContext.RestoreFlow();
                verifyEquality(asyncFlowControl0, asyncFlowControl1, true);
                verifyEquality(asyncFlowControl1, asyncFlowControl0, true);

                var asyncLocal    = new AsyncLocal <int>();
                asyncLocal.Value  = 1;
                asyncFlowControl1 = ExecutionContext.SuppressFlow();
                ExecutionContext.RestoreFlow();
                verifyEquality(asyncFlowControl0, asyncFlowControl1, true);
                verifyEquality(asyncFlowControl1, asyncFlowControl0, true);

                asyncFlowControl1 = new AsyncFlowControl();
                verifyEquality(asyncFlowControl0, asyncFlowControl1, false);
                verifyEquality(asyncFlowControl1, asyncFlowControl0, false);

                ThreadTestHelpers.RunTestInBackgroundThread(() => asyncFlowControl1 = ExecutionContext.SuppressFlow());
                verifyEquality(asyncFlowControl0, asyncFlowControl1, false);
                verifyEquality(asyncFlowControl1, asyncFlowControl0, false);
            });
        }
Exemplo n.º 9
0
 void UsingDisposeableStruct()
 {
     using (AsyncFlowControl flow = new AsyncFlowControl())
     {
     }
 }
 public static bool op_Inequality(AsyncFlowControl a, AsyncFlowControl b)
 {
 }
 public bool Equals(AsyncFlowControl obj)
 {
 }
Exemplo n.º 12
0
 public SuppressFlow()
 {
     _undoer = ExecutionContext.SuppressFlow();
 }
Exemplo n.º 13
0
 public AsyncFlowControlHelper(AsyncFlowControl asyncFlowControl)
 {
     _asyncFlowControl = asyncFlowControl;
 }
	public static bool op_Inequality(AsyncFlowControl a, AsyncFlowControl b) {}
	public bool Equals(AsyncFlowControl obj) {}