Ejemplo n.º 1
0
        [HandleProcessCorruptedStateExceptions] //
#endif // FEATURE_CORRUPTING_EXCEPTIONS
        internal static SecurityContextSwitcher SetSecurityContext(SecurityContext sc, SecurityContext.Reader prevSecurityContext, bool modifyCurrentExecutionContext, ref StackCrawlMark stackMark)
        {
            // Save the flow state at capture and reset it in the SC.
            SecurityContextDisableFlow _capturedFlowState = sc._disableFlow;

            sc._disableFlow = SecurityContextDisableFlow.Nothing;

            //Set up the switcher object
            SecurityContextSwitcher scsw = new SecurityContextSwitcher();

            scsw.currSC = sc;
            scsw.prevSC = prevSecurityContext;

            if (modifyCurrentExecutionContext)
            {
                // save the current Execution Context
                ExecutionContext currEC = Thread.CurrentThread.GetMutableExecutionContext();
                scsw.currEC            = currEC;
                currEC.SecurityContext = sc;
            }

            if (sc != null)
            {
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
#if !FEATURE_PAL && FEATURE_IMPERSONATION
                    scsw.wic = null;
                    if (!_LegacyImpersonationPolicy)
                    {
                        if (sc.WindowsIdentity != null)
                        {
                            scsw.wic = sc.WindowsIdentity.Impersonate(ref stackMark);
                        }
                        else if (((_capturedFlowState & SecurityContextDisableFlow.WI) == 0) &&
                                 prevSecurityContext.WindowsIdentity != null)
                        {
                            // revert impersonation if there was no WI flow supression at capture and we're currently impersonating
                            scsw.wic = WindowsIdentity.SafeRevertToSelf(ref stackMark);
                        }
                    }
#endif
                    scsw.cssw = CompressedStack.SetCompressedStack(sc.CompressedStack, prevSecurityContext.CompressedStack);
                }
                catch
                {
                    scsw.UndoNoThrow();
                    throw;
                }
            }
            return(scsw);
        }
Ejemplo n.º 2
0
        internal static SecurityContextSwitcher SetSecurityContext(SecurityContext sc, SecurityContext.Reader prevSecurityContext, bool modifyCurrentExecutionContext, ref StackCrawlMark stackMark)
        {
            SecurityContextDisableFlow disableFlow = sc._disableFlow;

            sc._disableFlow = SecurityContextDisableFlow.Nothing;
            SecurityContextSwitcher result = default(SecurityContextSwitcher);

            result.currSC = sc;
            result.prevSC = prevSecurityContext;
            if (modifyCurrentExecutionContext)
            {
                ExecutionContext mutableExecutionContext = Thread.CurrentThread.GetMutableExecutionContext();
                result.currEC = mutableExecutionContext;
                mutableExecutionContext.SecurityContext = sc;
            }
            if (sc != null)
            {
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    result.wic = null;
                    if (!SecurityContext._LegacyImpersonationPolicy)
                    {
                        if (sc.WindowsIdentity != null)
                        {
                            result.wic = sc.WindowsIdentity.Impersonate(ref stackMark);
                        }
                        else if ((disableFlow & SecurityContextDisableFlow.WI) == SecurityContextDisableFlow.Nothing && prevSecurityContext.WindowsIdentity != null)
                        {
                            result.wic = WindowsIdentity.SafeRevertToSelf(ref stackMark);
                        }
                    }
                    result.cssw = CompressedStack.SetCompressedStack(sc.CompressedStack, prevSecurityContext.CompressedStack);
                }
                catch
                {
                    result.UndoNoThrow();
                    throw;
                }
            }
            return(result);
        }