[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); }
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); }
internal static SecurityContextSwitcher SetSecurityContext(SecurityContext sc, SecurityContext prevSecurityContext, ref StackCrawlMark stackMark) { SecurityContextDisableFlow flow = sc._disableFlow; sc._disableFlow = SecurityContextDisableFlow.Nothing; SecurityContextSwitcher switcher = new SecurityContextSwitcher { currSC = sc }; System.Threading.ExecutionContext executionContext = Thread.CurrentThread.ExecutionContext; switcher.currEC = executionContext; switcher.prevSC = prevSecurityContext; executionContext.SecurityContext = sc; if (sc != null) { RuntimeHelpers.PrepareConstrainedRegions(); try { switcher.wic = null; if (!_LegacyImpersonationPolicy) { if (sc.WindowsIdentity != null) { switcher.wic = sc.WindowsIdentity.Impersonate(ref stackMark); } else if ((((flow & SecurityContextDisableFlow.WI) == SecurityContextDisableFlow.Nothing) && (prevSecurityContext != null)) && (prevSecurityContext.WindowsIdentity != null)) { switcher.wic = System.Security.Principal.WindowsIdentity.SafeRevertToSelf(ref stackMark); } } switcher.cssw = System.Threading.CompressedStack.SetCompressedStack(sc.CompressedStack, (prevSecurityContext != null) ? prevSecurityContext.CompressedStack : null); } catch { switcher.UndoNoThrow(); throw; } } return(switcher); }
internal static SecurityContextSwitcher SetSecurityContext(SecurityContext sc, SecurityContext prevSecurityContext, 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; // save the current Execution Context ExecutionContext currEC = Thread.CurrentThread.ExecutionContext; scsw.currEC = currEC; // save the prev security context scsw.prevSC = prevSecurityContext; // update the current security context to the new security context currEC.SecurityContext = sc; if (sc != null) { RuntimeHelpers.PrepareConstrainedRegions(); try { scsw.cssw = CompressedStack.SetCompressedStack(sc.CompressedStack, (prevSecurityContext != null?prevSecurityContext.CompressedStack:null)); } catch { scsw.UndoNoThrow(); throw; } } return(scsw); }
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 { scsw.cssw = CompressedStack.SetCompressedStack(sc.CompressedStack, prevSecurityContext.CompressedStack); } catch { scsw.UndoNoThrow(); throw; } } return(scsw); }
[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; }
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 { scsw.cssw = CompressedStack.SetCompressedStack(sc.CompressedStack, prevSecurityContext.CompressedStack); } catch { scsw.UndoNoThrow(); throw; } } return scsw; }
internal static SecurityContextSwitcher SetSecurityContext(SecurityContext sc, SecurityContext prevSecurityContext, 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; // save the current Execution Context ExecutionContext currEC = Thread.CurrentThread.ExecutionContext; scsw.currEC = currEC; // save the prev security context scsw.prevSC = prevSecurityContext; // update the current security context to the new security context currEC.SecurityContext = sc; if (sc != null) { RuntimeHelpers.PrepareConstrainedRegions(); try { scsw.cssw = CompressedStack.SetCompressedStack(sc.CompressedStack, (prevSecurityContext!=null?prevSecurityContext.CompressedStack:null)); } catch { scsw.UndoNoThrow(); throw; } } return scsw; }