public override object SetHostExecutionContext(HostExecutionContext hostExecutionContext)
        {
            var castHostExecutionContext = hostExecutionContext as DnxHostExecutionContext;
            if (castHostExecutionContext != null)
            {
                object baseRevertParameter = null;
                if (castHostExecutionContext.BaseContext != null)
                {
                    baseRevertParameter = base.SetHostExecutionContext(castHostExecutionContext.BaseContext);
                }

                // Capture the current status of the thread context and then update the values
                // The captured snapshot will be used to revert the status later
                var originalThreadContext = castHostExecutionContext.ThreadContext.CaptureAndReplace(Thread.CurrentThread);

                return (RevertAction)(() =>
                {
                    originalThreadContext.CaptureAndReplace(Thread.CurrentThread);
                    if (baseRevertParameter != null)
                    {
                        base.Revert(baseRevertParameter);
                    }
                });
            }
            else
            {
                return base.SetHostExecutionContext(hostExecutionContext);
            }
        }
Exemple #2
0
        internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext)
        {
            StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;

            BCLDebug.Assert(executionContext != null, "ExecutionContext cannot be null here.");


            // Set up the switcher object to return;
            ExecutionContextSwitcher ecsw = new ExecutionContextSwitcher();

            ecsw.thread = Thread.CurrentThread;
            ecsw.prevEC = Thread.CurrentThread.GetExecutionContextNoCreate(); // prev
            ecsw.currEC = executionContext;                                   //current

            // Update the EC on thread
            Thread.CurrentThread.SetExecutionContext(executionContext);

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                if (executionContext != null)
                {
                    //set the security context
                    SecurityContext sc = executionContext.SecurityContext;
                    if (sc != null)
                    {
                        // non-null SC: needs to be set
                        SecurityContext prevSeC = (ecsw.prevEC != null) ? ecsw.prevEC.SecurityContext : null;
                        ecsw.scsw = SecurityContext.SetSecurityContext(sc, prevSeC, ref stackMark);
                    }
                    else if (!SecurityContext.CurrentlyInDefaultFTSecurityContext(ecsw.prevEC))
                    {
                        // null incoming SC, but we're currently not in FT: use static FTSC to set
                        SecurityContext prevSeC = (ecsw.prevEC != null) ? ecsw.prevEC.SecurityContext : null;
                        ecsw.scsw = SecurityContext.SetSecurityContext(SecurityContext.FullTrustSecurityContext, prevSeC, ref stackMark);
                    }

                    // set the sync context
                    SynchronizationContext syncContext = executionContext.SynchronizationContext;
                    if (syncContext != null)
                    {
                        SynchronizationContext prevSyC = (ecsw.prevEC != null) ? ecsw.prevEC.SynchronizationContext : null;
                        ecsw.sysw = SynchronizationContext.SetSynchronizationContext(syncContext, prevSyC);
                    }

                    // set the Host Context
                    HostExecutionContext hostContext = executionContext.HostExecutionContext;
                    if (hostContext != null)
                    {
                        ecsw.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostContext);
                    }
                }
            }
            catch
            {
                ecsw.UndoNoThrow();
                throw;
            }
            return(ecsw);
        }
        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;
        }
Exemple #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;
        }
        internal static HostExecutionContext CaptureHostExecutionContext()
        {
            HostExecutionContext        result = null;
            HostExecutionContextManager currentHostExecutionContextManager = HostExecutionContextManager.GetCurrentHostExecutionContextManager();

            if (currentHostExecutionContextManager != null)
            {
                result = currentHostExecutionContextManager.Capture();
            }
            return(result);
        }
        internal static object SetHostExecutionContextInternal(HostExecutionContext hostContext)
        {
            HostExecutionContextManager currentHostExecutionContextManager = GetCurrentHostExecutionContextManager();
            object obj2 = null;

            if (currentHostExecutionContextManager != null)
            {
                obj2 = currentHostExecutionContextManager.SetHostExecutionContext(hostContext);
            }
            return(obj2);
        }
        internal static HostExecutionContext CaptureHostExecutionContext()
        {
            HostExecutionContext        context = null;
            HostExecutionContextManager currentHostExecutionContextManager = GetCurrentHostExecutionContextManager();

            if (currentHostExecutionContextManager != null)
            {
                context = currentHostExecutionContextManager.Capture();
            }
            return(context);
        }
 public virtual HostExecutionContext Capture()
 {
     HostExecutionContext context = null;
     if (CheckIfHosted())
     {
         IUnknownSafeHandle state = new IUnknownSafeHandle();
         context = new HostExecutionContext(state);
         CaptureHostSecurityContext(state);
     }
     return context;
 }
Exemple #10
0
        internal static object SetHostExecutionContextInternal(HostExecutionContext hostContext)
        {
            HostExecutionContextManager executionContextManager = HostExecutionContextManager.GetCurrentHostExecutionContextManager();
            object obj = (object)null;

            if (executionContextManager != null)
            {
                obj = executionContextManager.SetHostExecutionContext(hostContext);
            }
            return(obj);
        }
Exemple #11
0
        internal static HostExecutionContext CaptureHostExecutionContext()
        {
            HostExecutionContext        executionContext        = (HostExecutionContext)null;
            HostExecutionContextManager executionContextManager = HostExecutionContextManager.GetCurrentHostExecutionContextManager();

            if (executionContextManager != null)
            {
                executionContext = executionContextManager.Capture();
            }
            return(executionContext);
        }
        internal static object SetHostExecutionContextInternal(HostExecutionContext hostContext)
        {
            HostExecutionContextManager currentHostExecutionContextManager = HostExecutionContextManager.GetCurrentHostExecutionContextManager();
            object result = null;

            if (currentHostExecutionContextManager != null)
            {
                result = currentHostExecutionContextManager.SetHostExecutionContext(hostContext);
            }
            return(result);
        }
Exemple #13
0
        internal static Object SetHostExecutionContextInternal(HostExecutionContext hostContext)
        {
            HostExecutionContextManager hostMgr = HostExecutionContextManager.GetCurrentHostExecutionContextManager();
            Object switcher = null;

            if (hostMgr != null)
            {
                switcher = hostMgr.SetHostExecutionContext(hostContext);
            }
            return(switcher);
        }
Exemple #14
0
        public virtual HostExecutionContext Capture()
        {
            HostExecutionContext executionContext = (HostExecutionContext)null;

            if (HostExecutionContextManager.CheckIfHosted())
            {
                IUnknownSafeHandle iunknownSafeHandle = new IUnknownSafeHandle();
                executionContext = new HostExecutionContext((object)iunknownSafeHandle);
                HostExecutionContextManager.CaptureHostSecurityContext((SafeHandle)iunknownSafeHandle);
            }
            return(executionContext);
        }
        public virtual HostExecutionContext Capture()
        {
            HostExecutionContext result = null;

            if (HostExecutionContextManager.CheckIfHosted())
            {
                IUnknownSafeHandle unknownSafeHandle = new IUnknownSafeHandle();
                result = new HostExecutionContext(unknownSafeHandle);
                HostExecutionContextManager.CaptureHostSecurityContext(unknownSafeHandle);
            }
            return(result);
        }
Exemple #16
0
        internal static HostExecutionContext CaptureHostExecutionContext()
        {
            HostExecutionContext hostContext = null;
            // capture the host execution context
            HostExecutionContextManager hostMgr = HostExecutionContextManager.GetCurrentHostExecutionContextManager();

            if (hostMgr != null)
            {
                hostContext = hostMgr.Capture();
            }
            return(hostContext);
        }
        public virtual HostExecutionContext Capture()
        {
            HostExecutionContext context = null;

            if (CheckIfHosted())
            {
                IUnknownSafeHandle state = new IUnknownSafeHandle();
                context = new HostExecutionContext(state);
                CaptureHostSecurityContext(state);
            }
            return(context);
        }
        internal static ExecutionContext Capture(ref StackCrawlMark stackMark, ExecutionContext.CaptureOptions options)
        {
            ExecutionContext.Reader executionContextReader = Thread.CurrentThread.GetExecutionContextReader();
            if (executionContextReader.IsFlowSuppressed)
            {
                return((ExecutionContext)null);
            }
            SecurityContext        securityContext        = SecurityContext.Capture(executionContextReader, ref stackMark);
            HostExecutionContext   executionContext1      = HostExecutionContextManager.CaptureHostExecutionContext();
            SynchronizationContext synchronizationContext = (SynchronizationContext)null;
            LogicalCallContext     logicalCallContext     = (LogicalCallContext)null;

            if (!executionContextReader.IsNull)
            {
                if ((options & ExecutionContext.CaptureOptions.IgnoreSyncCtx) == ExecutionContext.CaptureOptions.None)
                {
                    synchronizationContext = executionContextReader.SynchronizationContext == null ? (SynchronizationContext)null : executionContextReader.SynchronizationContext.CreateCopy();
                }
                if (executionContextReader.LogicalCallContext.HasInfo)
                {
                    logicalCallContext = executionContextReader.LogicalCallContext.Clone();
                }
            }
            Dictionary <IAsyncLocal, object> dictionary = (Dictionary <IAsyncLocal, object>)null;
            List <IAsyncLocal> asyncLocalList           = (List <IAsyncLocal>)null;

            if (!executionContextReader.IsNull)
            {
                dictionary     = executionContextReader.DangerousGetRawExecutionContext()._localValues;
                asyncLocalList = executionContextReader.DangerousGetRawExecutionContext()._localChangeNotifications;
            }
            if ((options & ExecutionContext.CaptureOptions.OptimizeDefaultCase) != ExecutionContext.CaptureOptions.None && securityContext == null && (executionContext1 == null && synchronizationContext == null) && ((logicalCallContext == null || !logicalCallContext.HasInfo) && (dictionary == null && asyncLocalList == null)))
            {
                return(ExecutionContext.s_dummyDefaultEC);
            }
            ExecutionContext executionContext2 = new ExecutionContext();

            executionContext2.SecurityContext = securityContext;
            if (executionContext2.SecurityContext != null)
            {
                executionContext2.SecurityContext.ExecutionContext = executionContext2;
            }
            executionContext2._hostExecutionContext     = executionContext1;
            executionContext2._syncContext              = synchronizationContext;
            executionContext2.LogicalCallContext        = logicalCallContext;
            executionContext2._localValues              = dictionary;
            executionContext2._localChangeNotifications = asyncLocalList;
            executionContext2.isNewCapture              = true;
            return(executionContext2);
        }
Exemple #19
0
        internal static ExecutionContext Capture(ref StackCrawlMark stackMark, ExecutionContext.CaptureOptions options)
        {
            ExecutionContext.Reader executionContextReader = Thread.CurrentThread.GetExecutionContextReader();
            if (executionContextReader.IsFlowSuppressed)
            {
                return(null);
            }
            SecurityContext        securityContext        = SecurityContext.Capture(executionContextReader, ref stackMark);
            HostExecutionContext   hostExecutionContext   = HostExecutionContextManager.CaptureHostExecutionContext();
            SynchronizationContext synchronizationContext = null;
            LogicalCallContext     logicalCallContext     = null;

            if (!executionContextReader.IsNull)
            {
                if ((options & ExecutionContext.CaptureOptions.IgnoreSyncCtx) == ExecutionContext.CaptureOptions.None)
                {
                    synchronizationContext = ((executionContextReader.SynchronizationContext == null) ? null : executionContextReader.SynchronizationContext.CreateCopy());
                }
                if (executionContextReader.LogicalCallContext.HasInfo)
                {
                    logicalCallContext = executionContextReader.LogicalCallContext.Clone();
                }
            }
            IAsyncLocalValueMap asyncLocalValueMap = null;

            IAsyncLocal[] array = null;
            if (!executionContextReader.IsNull)
            {
                asyncLocalValueMap = executionContextReader.DangerousGetRawExecutionContext()._localValues;
                array = executionContextReader.DangerousGetRawExecutionContext()._localChangeNotifications;
            }
            if ((options & ExecutionContext.CaptureOptions.OptimizeDefaultCase) != ExecutionContext.CaptureOptions.None && securityContext == null && hostExecutionContext == null && synchronizationContext == null && (logicalCallContext == null || !logicalCallContext.HasInfo) && asyncLocalValueMap == null && array == null)
            {
                return(ExecutionContext.s_dummyDefaultEC);
            }
            ExecutionContext executionContext = new ExecutionContext();

            executionContext.SecurityContext = securityContext;
            if (executionContext.SecurityContext != null)
            {
                executionContext.SecurityContext.ExecutionContext = executionContext;
            }
            executionContext._hostExecutionContext     = hostExecutionContext;
            executionContext._syncContext              = synchronizationContext;
            executionContext.LogicalCallContext        = logicalCallContext;
            executionContext._localValues              = asyncLocalValueMap;
            executionContext._localChangeNotifications = array;
            executionContext.isNewCapture              = true;
            return(executionContext);
        }
Exemple #20
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public virtual HostExecutionContext Capture()
        {
            HostExecutionContext context = null;

            // check if we are hosted
            if (CheckIfHosted())
            {
                IUnknownSafeHandle unkSafeHandle = new IUnknownSafeHandle();
                context = new HostExecutionContext(unkSafeHandle);

                // call into the Hosting API to capture the host context
                // stores the output IUnknown in the safehandle,
                CaptureHostSecurityContext(unkSafeHandle);
            }

            // otherwise
            return(context);
        }
        internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext, bool preserveSyncCtx)
        {
            StackCrawlMark           stackMark = StackCrawlMark.LookForMyCaller;
            ExecutionContextSwitcher executionContextSwitcher = new ExecutionContextSwitcher();
            Thread currentThread = Thread.CurrentThread;

            ExecutionContext.Reader executionContextReader = currentThread.GetExecutionContextReader();
            executionContextSwitcher.thread  = currentThread;
            executionContextSwitcher.outerEC = executionContextReader;
            executionContextSwitcher.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope;
            if (preserveSyncCtx)
            {
                executionContext.SynchronizationContext = executionContextReader.SynchronizationContext;
            }
            executionContext.SynchronizationContextNoFlow = executionContextReader.SynchronizationContextNoFlow;
            currentThread.SetExecutionContext(executionContext, true);
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                ExecutionContext.OnAsyncLocalContextChanged(executionContextReader.DangerousGetRawExecutionContext(), executionContext);
                SecurityContext securityContext1 = executionContext.SecurityContext;
                if (securityContext1 != null)
                {
                    SecurityContext.Reader securityContext2 = executionContextReader.SecurityContext;
                    executionContextSwitcher.scsw = SecurityContext.SetSecurityContext(securityContext1, securityContext2, false, ref stackMark);
                }
                else if (!SecurityContext.CurrentlyInDefaultFTSecurityContext(executionContextSwitcher.outerEC))
                {
                    SecurityContext.Reader securityContext2 = executionContextReader.SecurityContext;
                    executionContextSwitcher.scsw = SecurityContext.SetSecurityContext(SecurityContext.FullTrustSecurityContext, securityContext2, false, ref stackMark);
                }
                HostExecutionContext executionContext1 = executionContext.HostExecutionContext;
                if (executionContext1 != null)
                {
                    executionContextSwitcher.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(executionContext1);
                }
            }
            catch
            {
                executionContextSwitcher.UndoNoThrow();
                throw;
            }
            return(executionContextSwitcher);
        }
Exemple #22
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;
        }
Exemple #23
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 override object SetHostExecutionContext(HostExecutionContext hostExecutionContext) {
            AspNetHostExecutionContext castHostExecutionContext = hostExecutionContext as AspNetHostExecutionContext;
            if (castHostExecutionContext != null) {
                // Call base.SetHostExecutionContext before calling our own logic.
                object baseRevertParameter = null;
                if (castHostExecutionContext.BaseContext != null) {
                    baseRevertParameter = base.SetHostExecutionContext(castHostExecutionContext.BaseContext);
                }

                ThreadContext currentContext = ThreadContext.Current;
                if (currentContext != null && currentContext.HttpContext.ThreadContextId == castHostExecutionContext.HttpContextThreadContextId) {
                    // If we reached this point, then 'castHostExecutionContext' was captured for the HttpContext
                    // that is associated with the ThreadContext that is assigned to the current thread. We can
                    // safely restore it.
                    Action threadContextCleanupAction = currentContext.EnterExecutionContext();

                    // Perform cleanup in the opposite order from initialization.
                    return (RevertAction)(() => {
                        threadContextCleanupAction();
                        if (baseRevertParameter != null) {
                            base.Revert(baseRevertParameter);
                        }
                    });
                }
                else {
                    // If we reached this point, then 'castHostExecutionContext' was captured by us
                    // but is not applicable to the current thread. This can happen if the developer
                    // called ThreadPool.QueueUserWorkItem, for example. We don't restore HttpContext
                    // on such threads since they're not under the control of ASP.NET. In this case,
                    // we have already called base.SetHostExecutionContext, so we just need to return
                    // the result of that function directly to our caller.
                    return baseRevertParameter;
                }
            }
            else {
                // If we reached this point, then 'hostExecutionContext' was generated by our
                // base class instead of by us, so just delegate to the base implementation.
                return base.SetHostExecutionContext(hostExecutionContext);
            }
        }
        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);
        }
Exemple #26
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);
        }
 public virtual new Object SetHostExecutionContext(HostExecutionContext hostExecutionContext)
 {
     return(default(Object));
 }
        [System.Security.SecuritySafeCritical]  // auto-generated
        public virtual  HostExecutionContext Capture()
        {
            HostExecutionContext context =  null;
            // check if we are hosted
            if (CheckIfHosted())
            {
                IUnknownSafeHandle unkSafeHandle = new IUnknownSafeHandle();    
                            context = new HostExecutionContext(unkSafeHandle);
                            
                // call into the Hosting API to capture the host context                            
                // stores the output IUnknown in the safehandle,
                CaptureHostSecurityContext(unkSafeHandle);    
            }

            // otherwise
            return context;
            
        }
 public virtual new Object SetHostExecutionContext(HostExecutionContext hostExecutionContext)
 {
   return default(Object);
 }
		public virtual object SetHostExecutionContext (HostExecutionContext hostExecutionContext)
		{
			throw new NotImplementedException ();
		}
 internal static object SetHostExecutionContextInternal(HostExecutionContext hostContext)
 {
     HostExecutionContextManager currentHostExecutionContextManager = GetCurrentHostExecutionContextManager();
     object obj2 = null;
     if (currentHostExecutionContextManager != null)
     {
         obj2 = currentHostExecutionContextManager.SetHostExecutionContext(hostContext);
     }
     return obj2;
 }
 internal DnxHostExecutionContext(HostExecutionContext baseContext, ThreadContext threadContext)
 {
     BaseContext = baseContext;
     ThreadContext = threadContext;
 }
 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;
 }
 internal AspNetHostExecutionContext(HostExecutionContext baseContext, object httpContextThreadContextId) {
     BaseContext = baseContext;
     HttpContextThreadContextId = httpContextThreadContextId;
 }
 [System.Security.SecurityCritical]  // auto-generated
 internal static Object SetHostExecutionContextInternal(HostExecutionContext hostContext)
 {
     HostExecutionContextManager hostMgr = HostExecutionContextManager.GetCurrentHostExecutionContextManager();
     Object switcher = null;
     if (hostMgr != null) 
     {  
         switcher = hostMgr.SetHostExecutionContext(hostContext);
         // 
     }
     return switcher;
 }
 private static HostExecutionContext CreateCopyHelper(HostExecutionContext hostExecutionContext)
 {
     return (hostExecutionContext != null) ? hostExecutionContext.CreateCopy() : null;
 }
 private static HostExecutionContext CreateCopyHelper(HostExecutionContext hostExecutionContext) {
     // creating a copy of a null context should just itself return null
     return (hostExecutionContext != null) ? hostExecutionContext.CreateCopy() : null;
 }
        [HandleProcessCorruptedStateExceptions]             //
#endif // FEATURE_CORRUPTING_EXCEPTIONS
        internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext, bool preserveSyncCtx)
        {
#if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
            StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
#endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK

            Contract.Assert(executionContext != null);
            Contract.Assert(executionContext != s_dummyDefaultEC);

            // Set up the switcher object to return;
            ExecutionContextSwitcher ecsw = new ExecutionContextSwitcher();

            Thread currentThread            = Thread.CurrentThread;
            ExecutionContext.Reader outerEC = currentThread.GetExecutionContextReader();

            ecsw.thread  = currentThread;
            ecsw.outerEC = outerEC;
            ecsw.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope;

            if (preserveSyncCtx)
            {
                executionContext.SynchronizationContext = outerEC.SynchronizationContext;
            }
            executionContext.SynchronizationContextNoFlow = outerEC.SynchronizationContextNoFlow;

            currentThread.SetExecutionContext(executionContext, belongsToCurrentScope: true);

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
#if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
                //set the security context
                SecurityContext sc = executionContext.SecurityContext;
                if (sc != null)
                {
                    // non-null SC: needs to be set
                    SecurityContext.Reader prevSeC = outerEC.SecurityContext;
                    ecsw.scsw = SecurityContext.SetSecurityContext(sc, prevSeC, false, ref stackMark);
                }
                else if (!SecurityContext.CurrentlyInDefaultFTSecurityContext(ecsw.outerEC))
                {
                    // null incoming SC, but we're currently not in FT: use static FTSC to set
                    SecurityContext.Reader prevSeC = outerEC.SecurityContext;
                    ecsw.scsw = SecurityContext.SetSecurityContext(SecurityContext.FullTrustSecurityContext, prevSeC, false, ref stackMark);
                }
#endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
#if FEATURE_CAS_POLICY
                // set the Host Context
                HostExecutionContext hostContext = executionContext.HostExecutionContext;
                if (hostContext != null)
                {
                    ecsw.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostContext);
                }
#endif // FEATURE_CAS_POLICY
            }
            catch
            {
                ecsw.UndoNoThrow();
                throw;
            }
            return(ecsw);
        }
        [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;            
        }
Exemple #40
0
 public virtual object SetHostExecutionContext(HostExecutionContext hostExecutionContext)
 {
     throw new NotImplementedException();
 }
        [System.Security.SecurityCritical]  // auto-generated
        static internal ExecutionContext Capture(ref StackCrawlMark stackMark, CaptureOptions options)
        {
            ExecutionContext.Reader ecCurrent = Thread.CurrentThread.GetExecutionContextReader();

            // check to see if Flow is suppressed
            if (ecCurrent.IsFlowSuppressed)
            {
                return(null);
            }

            //
            // Attempt to capture context.  There may be nothing to capture...
            //

#if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
            // capture the security context
            SecurityContext secCtxNew = SecurityContext.Capture(ecCurrent, ref stackMark);
#endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
#if FEATURE_CAS_POLICY
            // capture the host execution context
            HostExecutionContext hostCtxNew = HostExecutionContextManager.CaptureHostExecutionContext();
#endif // FEATURE_CAS_POLICY

#if FEATURE_SYNCHRONIZATIONCONTEXT
            SynchronizationContext syncCtxNew = null;
#endif
            LogicalCallContext logCtxNew = null;

            if (!ecCurrent.IsNull)
            {
#if FEATURE_SYNCHRONIZATIONCONTEXT
                // capture the [....] context
                if (0 == (options & CaptureOptions.IgnoreSyncCtx))
                {
                    syncCtxNew = (ecCurrent.SynchronizationContext == null) ? null : ecCurrent.SynchronizationContext.CreateCopy();
                }
#endif // #if FEATURE_SYNCHRONIZATIONCONTEXT

                // copy over the Logical Call Context
                if (ecCurrent.LogicalCallContext.HasInfo)
                {
                    logCtxNew = ecCurrent.LogicalCallContext.Clone();
                }
            }

            //
            // If we didn't get anything but defaults, and we're allowed to return the
            // dummy default EC, don't bother allocating a new context.
            //
            if (0 != (options & CaptureOptions.OptimizeDefaultCase) &&
#if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
                secCtxNew == null &&
#endif
#if FEATURE_CAS_POLICY
                hostCtxNew == null &&
#endif // FEATURE_CAS_POLICY
#if FEATURE_SYNCHRONIZATIONCONTEXT
                syncCtxNew == null &&
#endif // #if FEATURE_SYNCHRONIZATIONCONTEXT
                (logCtxNew == null || !logCtxNew.HasInfo))
            {
                return(s_dummyDefaultEC);
            }

            //
            // Allocate the new context, and fill it in.
            //
            ExecutionContext ecNew = new ExecutionContext();
#if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
            ecNew.SecurityContext = secCtxNew;
            if (ecNew.SecurityContext != null)
            {
                ecNew.SecurityContext.ExecutionContext = ecNew;
            }
#endif
#if FEATURE_CAS_POLICY
            ecNew._hostExecutionContext = hostCtxNew;
#endif // FEATURE_CAS_POLICY
#if FEATURE_SYNCHRONIZATIONCONTEXT
            ecNew._syncContext = syncCtxNew;
#endif // #if FEATURE_SYNCHRONIZATIONCONTEXT
            ecNew.LogicalCallContext = logCtxNew;
            ecNew.isNewCapture       = true;

            return(ecNew);
        }