Example #1
0
        [HandleProcessCorruptedStateExceptions]                             //
#endif // FEATURE_CORRUPTING_EXCEPTIONS
        public void Undo()
        {
            if (currSC == null)
            {
                return; // mutiple Undo()s called on this switcher object
            }

            if (currEC != null)
            {
                Contract.Assert(currEC == Thread.CurrentThread.GetMutableExecutionContext(), "SecurityContextSwitcher used from another thread");
                Contract.Assert(currSC == currEC.SecurityContext, "SecurityContextSwitcher context mismatch");

                // restore the saved security context
                currEC.SecurityContext = prevSC.DangerousGetRawSecurityContext();
            }
            else
            {
                // caller must have already restored the ExecutionContext
                Contract.Assert(Thread.CurrentThread.GetExecutionContextReader().SecurityContext.IsSame(prevSC));
            }

            currSC = null; // this will prevent the switcher object being used again

            bool bNoException = true;

#if !FEATURE_PAL && FEATURE_IMPERSONATION
            try
            {
                if (wic != null)
                {
                    bNoException &= wic.UndoNoThrow();
                }
            }
            catch
            {
                // Failfast since we can't continue safely...
                bNoException &= cssw.UndoNoThrow();
                System.Environment.FailFast(Environment.GetResourceString("ExecutionContext_UndoFailed"));
            }
#endif
            bNoException &= cssw.UndoNoThrow();


            if (!bNoException)
            {
                // Failfast since we can't continue safely...
                System.Environment.FailFast(Environment.GetResourceString("ExecutionContext_UndoFailed"));
            }
        }