[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);
}