Esempio n. 1
0
        internal void Undo()
        {
            if (this.thread == null)
            {
                return;
            }
            Thread thread = this.thread;

            if (this.hecsw != null)
            {
                HostExecutionContextSwitcher.Undo(this.hecsw);
            }
            ExecutionContext.Reader executionContextReader = thread.GetExecutionContextReader();
            ExecutionContext.Reader reader = this.outerEC;
            int num = this.outerECBelongsToScope ? 1 : 0;

            thread.SetExecutionContext(reader, num != 0);
            if (this.scsw.currSC != null)
            {
                this.scsw.Undo();
            }
            if (this.wiIsValid)
            {
                SecurityContext.RestoreCurrentWI(this.outerEC, executionContextReader, this.wi, this.cachedAlwaysFlowImpersonationPolicy);
            }
            this.thread = (Thread)null;
            ExecutionContext.OnAsyncLocalContextChanged(executionContextReader.DangerousGetRawExecutionContext(), this.outerEC.DangerousGetRawExecutionContext());
        }
Esempio n. 2
0
 public void Undo()
 {
     if (this.thread != null)
     {
         if (this.thread != Thread.CurrentThread)
         {
             throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
         }
         if (this.currEC != Thread.CurrentThread.GetExecutionContextNoCreate())
         {
             throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_SwitcherCtxMismatch"));
         }
         this.scsw.Undo();
         try
         {
             HostExecutionContextSwitcher.Undo(this.hecsw);
         }
         finally
         {
             this.sysw.Undo();
         }
         Thread.CurrentThread.SetExecutionContext(this.prevEC);
         this.thread = null;
     }
 }
        public virtual void Revert(object previousState)
        {
            HostExecutionContextSwitcher switcher = previousState as HostExecutionContextSwitcher;

            if (switcher == null)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotOverrideSetWithoutRevert"));
            }
            ExecutionContext executionContext = Thread.CurrentThread.ExecutionContext;

            if (executionContext != switcher.executionContext)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
            }
            switcher.executionContext = null;
            if (executionContext.HostExecutionContext != switcher.currentHostContext)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
            }
            HostExecutionContext previousHostContext = switcher.previousHostContext;

            if ((CheckIfHosted() && (previousHostContext != null)) && (previousHostContext.State is IUnknownSafeHandle))
            {
                IUnknownSafeHandle state = (IUnknownSafeHandle)previousHostContext.State;
                SetHostSecurityContext(state, false, null);
            }
            executionContext.HostExecutionContext = previousHostContext;
        }
Esempio n. 4
0
        public virtual void Revert(object previousState)
        {
            HostExecutionContextSwitcher executionContextSwitcher = previousState as HostExecutionContextSwitcher;

            if (executionContextSwitcher == null)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotOverrideSetWithoutRevert"));
            }
            ExecutionContext executionContext1 = Thread.CurrentThread.GetMutableExecutionContext();
            ExecutionContext executionContext2 = executionContextSwitcher.executionContext;

            if (executionContext1 != executionContext2)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
            }
            executionContextSwitcher.executionContext = (ExecutionContext)null;
            if (executionContext1.HostExecutionContext != executionContextSwitcher.currentHostContext)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
            }
            HostExecutionContext executionContext3 = executionContextSwitcher.previousHostContext;

            if (HostExecutionContextManager.CheckIfHosted() && executionContext3 != null && executionContext3.State is IUnknownSafeHandle)
            {
                HostExecutionContextManager.SetHostSecurityContext((SafeHandle)executionContext3.State, false, (SafeHandle)null);
            }
            HostExecutionContext executionContext4 = executionContext3;

            executionContext1.HostExecutionContext = executionContext4;
        }
        public virtual void Revert(object previousState)
        {
            HostExecutionContextSwitcher hostExecutionContextSwitcher = previousState as HostExecutionContextSwitcher;

            if (hostExecutionContextSwitcher == null)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotOverrideSetWithoutRevert"));
            }
            ExecutionContext mutableExecutionContext = Thread.CurrentThread.GetMutableExecutionContext();

            if (mutableExecutionContext != hostExecutionContextSwitcher.executionContext)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
            }
            hostExecutionContextSwitcher.executionContext = null;
            HostExecutionContext hostExecutionContext = mutableExecutionContext.HostExecutionContext;

            if (hostExecutionContext != hostExecutionContextSwitcher.currentHostContext)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
            }
            HostExecutionContext previousHostContext = hostExecutionContextSwitcher.previousHostContext;

            if (HostExecutionContextManager.CheckIfHosted() && previousHostContext != null && previousHostContext.State is IUnknownSafeHandle)
            {
                IUnknownSafeHandle context = (IUnknownSafeHandle)previousHostContext.State;
                HostExecutionContextManager.SetHostSecurityContext(context, false, null);
            }
            mutableExecutionContext.HostExecutionContext = previousHostContext;
        }
Esempio n. 6
0
        public virtual void Revert(Object previousState)
        {
            HostExecutionContextSwitcher hostContextSwitcher = previousState as HostExecutionContextSwitcher;

            if (hostContextSwitcher == null)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotOverrideSetWithoutRevert"));
            }

            // check Undo is happening on the correct thread
            ExecutionContext executionContext = Thread.CurrentThread.GetMutableExecutionContext();

            if (executionContext != hostContextSwitcher.executionContext)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
            }
            hostContextSwitcher.executionContext = null; // Make sure switcher cannot be re-used.

            HostExecutionContext revertFromHostContext = executionContext.HostExecutionContext;

            // if the current host context is not the same as the one in the switcher, then revert is being called out of order
            if (revertFromHostContext != hostContextSwitcher.currentHostContext)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
            }

            // get the previous host context
            HostExecutionContext revertToHostContext = hostContextSwitcher.previousHostContext;

            // now check if we are hosted and revert the context in the host
            if (CheckIfHosted())
            {
                // try restore the previous context as the current context
                if (revertToHostContext != null && revertToHostContext.State is IUnknownSafeHandle)
                {
                    IUnknownSafeHandle unkprevSafeHandle = (IUnknownSafeHandle)revertToHostContext.State;
                    // call into the Hosting API to set the host context
                    SetHostSecurityContext(unkprevSafeHandle, false, null);
                }
            }

            //restore the previous host context in the executioncontext
            executionContext.HostExecutionContext = revertToHostContext;
        }
Esempio n. 7
0
        public virtual object SetHostExecutionContext(HostExecutionContext hostExecutionContext)
        {
            if (hostExecutionContext == null)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotNewCaptureContext"));
            }
            HostExecutionContextSwitcher executionContextSwitcher = new HostExecutionContextSwitcher();
            ExecutionContext             executionContext         = Thread.CurrentThread.GetMutableExecutionContext();

            executionContextSwitcher.executionContext    = executionContext;
            executionContextSwitcher.currentHostContext  = hostExecutionContext;
            executionContextSwitcher.previousHostContext = (HostExecutionContext)null;
            if (HostExecutionContextManager.CheckIfHosted() && hostExecutionContext.State is IUnknownSafeHandle)
            {
                IUnknownSafeHandle iunknownSafeHandle = new IUnknownSafeHandle();
                executionContextSwitcher.previousHostContext = new HostExecutionContext((object)iunknownSafeHandle);
                HostExecutionContextManager.SetHostSecurityContext((SafeHandle)hostExecutionContext.State, true, (SafeHandle)iunknownSafeHandle);
            }
            executionContext.HostExecutionContext = hostExecutionContext;
            return((object)executionContextSwitcher);
        }
        public virtual object SetHostExecutionContext(HostExecutionContext hostExecutionContext)
        {
            if (hostExecutionContext == null)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotNewCaptureContext"));
            }
            HostExecutionContextSwitcher switcher         = new HostExecutionContextSwitcher();
            ExecutionContext             executionContext = Thread.CurrentThread.ExecutionContext;

            switcher.executionContext    = executionContext;
            switcher.currentHostContext  = hostExecutionContext;
            switcher.previousHostContext = null;
            if (CheckIfHosted() && (hostExecutionContext.State is IUnknownSafeHandle))
            {
                IUnknownSafeHandle state = new IUnknownSafeHandle();
                switcher.previousHostContext = new HostExecutionContext(state);
                IUnknownSafeHandle context = (IUnknownSafeHandle)hostExecutionContext.State;
                SetHostSecurityContext(context, true, state);
            }
            executionContext.HostExecutionContext = hostExecutionContext;
            return(switcher);
        }
Esempio n. 9
0
        [System.Security.SecurityCritical]  // auto-generated_required
        public virtual Object SetHostExecutionContext(HostExecutionContext hostExecutionContext)
        {
            if (hostExecutionContext == null)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotNewCaptureContext"));
            }
            Contract.EndContractBlock();

            HostExecutionContextSwitcher switcher = new HostExecutionContextSwitcher();
            ExecutionContext             currentExecutionContext = Thread.CurrentThread.GetMutableExecutionContext();

            switcher.executionContext    = currentExecutionContext;
            switcher.currentHostContext  = hostExecutionContext;
            switcher.previousHostContext = null;

            if (CheckIfHosted())
            {
                if (hostExecutionContext.State is IUnknownSafeHandle)
                {
                    // setup the previous unknown handle
                    IUnknownSafeHandle unkPrevSafeHandle = new IUnknownSafeHandle();
                    switcher.previousHostContext = new HostExecutionContext(unkPrevSafeHandle);

                    // get the current handle
                    IUnknownSafeHandle unkSafeHandle = (IUnknownSafeHandle)hostExecutionContext.State;

                    // call into the Hosting API to set the host context
                    // second arg indicates whether we want to retrieve the previous context
                    SetHostSecurityContext(unkSafeHandle, true, unkPrevSafeHandle);
                }
            }

            // store the current HostExecutionContext in the ExecutionContext.
            currentExecutionContext.HostExecutionContext = hostExecutionContext;

            return(switcher);
        }
Esempio n. 10
0
        public void Undo()
        {
            if (thread == null)
            {
                return; // Don't do anything
            }
            if (thread != Thread.CurrentThread)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotUseSwitcherOtherThread"));
            }
            if (currEC != Thread.CurrentThread.GetExecutionContextNoCreate())
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_SwitcherCtxMismatch"));
            }
            BCLDebug.Assert(currEC != null, " ExecutionContext can't be null");


            // Any critical failure inside scsw will cause FailFast
            scsw.Undo();

            try
            {
                HostExecutionContextSwitcher.Undo(hecsw);
            }
            finally
            {
                // Even if HostExecutionContextSwitcher.Undo(hecsw) throws, we need to revert
                // synchronizationContext. If that throws, we'll be throwing an ex during an exception
                // unwind. That's OK - we'll just have nested exceptions.
                sysw.Undo();
            }

            // restore the saved Execution Context
            Thread.CurrentThread.SetExecutionContext(prevEC);
            thread = null; // this will prevent the switcher object being used again
        }
 public virtual object SetHostExecutionContext(HostExecutionContext hostExecutionContext)
 {
     if (hostExecutionContext == null)
     {
         throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotNewCaptureContext"));
     }
     HostExecutionContextSwitcher switcher = new HostExecutionContextSwitcher();
     ExecutionContext executionContext = Thread.CurrentThread.ExecutionContext;
     switcher.executionContext = executionContext;
     switcher.currentHostContext = hostExecutionContext;
     switcher.previousHostContext = null;
     if (CheckIfHosted() && (hostExecutionContext.State is IUnknownSafeHandle))
     {
         IUnknownSafeHandle state = new IUnknownSafeHandle();
         switcher.previousHostContext = new HostExecutionContext(state);
         IUnknownSafeHandle context = (IUnknownSafeHandle) hostExecutionContext.State;
         SetHostSecurityContext(context, true, state);
     }
     executionContext.HostExecutionContext = hostExecutionContext;
     return switcher;
 }
        [System.Security.SecurityCritical]  // auto-generated_required
        public virtual Object SetHostExecutionContext(HostExecutionContext hostExecutionContext)
        {            
            if (hostExecutionContext == null) 
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NotNewCaptureContext"));
            }
            Contract.EndContractBlock();
            
            HostExecutionContextSwitcher switcher = new HostExecutionContextSwitcher();    
            ExecutionContext currentExecutionContext = Thread.CurrentThread.GetMutableExecutionContext();
                     
            switcher.executionContext = currentExecutionContext;    
            switcher.currentHostContext = hostExecutionContext;     
                     switcher.previousHostContext = null;
            
            if (CheckIfHosted())
            {                        
                if (hostExecutionContext.State is IUnknownSafeHandle)
                {    
                                    // setup the previous unknown handle
                                   IUnknownSafeHandle unkPrevSafeHandle = new IUnknownSafeHandle();
                            switcher.previousHostContext = new HostExecutionContext(unkPrevSafeHandle);    

                                    // get the current handle
                    IUnknownSafeHandle unkSafeHandle  = (IUnknownSafeHandle)hostExecutionContext.State;
                    
                                   // call into the Hosting API to set the host context
                                   // second arg indicates whether we want to retrieve the previous context
                      SetHostSecurityContext(unkSafeHandle,true,unkPrevSafeHandle);                                   
                }                                                                                
            }

                    // store the current HostExecutionContext in the ExecutionContext.
            currentExecutionContext.HostExecutionContext = hostExecutionContext;                
            
            return switcher;            
        }
Esempio n. 13
0
        internal void Undo(Thread currentThread)
        {
            //
            // Don't use an uninitialized switcher, or one that's already been used.
            //
            if (thread == null)
            {
                return; // Don't do anything
            }
            Contract.Assert(currentThread == this.thread);

            //
            // Restore the HostExecutionContext before restoring the ExecutionContext.
            //
#if FEATURE_CAS_POLICY
            if (hecsw != null)
            {
                HostExecutionContextSwitcher.Undo(hecsw);
            }
#endif // FEATURE_CAS_POLICY

            //
            // restore the saved Execution Context.  Note that this will also restore the
            // SynchronizationContext, Logical/IllogicalCallContext, etc.
            //
#if !FEATURE_PAL && FEATURE_IMPERSONATION
            ExecutionContext.Reader innerEC = currentThread.GetExecutionContextReader();
#endif
            currentThread.SetExecutionContext(outerEC, outerECBelongsToScope);

#if DEBUG
            try
            {
                currentThread.ForbidExecutionContextMutation = true;
#endif
            //
            // Tell the SecurityContext to do the side-effects of restoration.
            //
#if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
            if (scsw.currSC != null)
            {
                // Any critical failure inside scsw will cause FailFast
                scsw.Undo();
            }
#endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK

#if !FEATURE_PAL && FEATURE_IMPERSONATION
            if (wiIsValid)
            {
                SecurityContext.RestoreCurrentWI(outerEC, innerEC, wi, cachedAlwaysFlowImpersonationPolicy);
            }
#endif

            thread = null;     // this will prevent the switcher object being used again
#if DEBUG
        }

        finally
        {
            currentThread.ForbidExecutionContextMutation = false;
        }
#endif
        }