2 using Microsoft.Win32.SafeHandles;
14 internal struct Reader
18 public bool IsNull => m_sc ==
null;
26 return m_sc.CompressedStack;
39 return m_sc.WindowsIdentity;
60 public bool IsSame(Reader sc)
62 return m_sc == sc.m_sc;
70 return (m_sc._disableFlow & flags) == flags;
76 internal class SecurityContextRunData
82 internal object state;
84 internal SecurityContextSwitcher scsw;
91 scsw =
default(SecurityContextSwitcher);
95 private static bool _LegacyImpersonationPolicy = GetImpersonationFlowMode() == WindowsImpersonationFlowMode.IMP_NOFLOW;
97 private static bool _alwaysFlowImpersonationPolicy = GetImpersonationFlowMode() == WindowsImpersonationFlowMode.IMP_ALWAYSFLOW;
107 internal volatile bool isNewCapture;
109 internal volatile SecurityContextDisableFlow _disableFlow;
120 if (_fullTrustSC ==
null)
122 _fullTrustSC = CreateFullTrustSecurityContext();
130 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
133 _executionContext = value;
141 return _windowsIdentity;
145 _windowsIdentity = value;
153 return _compressedStack;
155 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
158 _compressedStack = value;
162 internal static bool AlwaysFlowImpersonationPolicy => _alwaysFlowImpersonationPolicy;
164 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
172 if (_windowsIdentity !=
null)
202 if (mutableExecutionContext.SecurityContext ==
null)
204 mutableExecutionContext.SecurityContext =
new SecurityContext();
213 [SecuritySafeCritical]
217 if (securityContext ==
null || securityContext._disableFlow == SecurityContextDisableFlow.Nothing)
221 securityContext._disableFlow = SecurityContextDisableFlow.Nothing;
237 if (!_LegacyImpersonationPolicy)
244 [SecuritySafeCritical]
247 return Thread.
CurrentThread.GetExecutionContextReader().SecurityContext.IsFlowSuppressed(flags);
262 if (securityContext ==
null)
266 StackCrawlMark stackMark = StackCrawlMark.LookForMe;
267 if (!securityContext.isNewCapture)
271 securityContext.isNewCapture =
false;
273 if (CurrentlyInDefaultFTSecurityContext(executionContextReader) && securityContext.IsDefaultFTSecurityContext())
283 RunInternal(securityContext, callback, state);
288 internal static void RunInternal(
SecurityContext securityContext, ContextCallback callBack,
object state)
290 if (cleanupCode ==
null)
292 tryCode = runTryCode;
293 cleanupCode = runFinallyCode;
295 SecurityContextRunData userData =
new SecurityContextRunData(securityContext, callBack, state);
300 internal static void runTryCode(
object userData)
302 SecurityContextRunData securityContextRunData = (SecurityContextRunData)userData;
303 securityContextRunData.scsw = SetSecurityContext(securityContextRunData.sc,
Thread.
CurrentThread.GetExecutionContextReader().SecurityContext, modifyCurrentExecutionContext:
true);
304 securityContextRunData.callBack(securityContextRunData.state);
309 internal static void runFinallyCode(
object userData,
bool exceptionThrown)
311 SecurityContextRunData securityContextRunData = (SecurityContextRunData)userData;
312 securityContextRunData.scsw.Undo();
316 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail)]
318 internal static SecurityContextSwitcher SetSecurityContext(SecurityContext sc, Reader prevSecurityContext,
bool modifyCurrentExecutionContext)
320 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
321 return SetSecurityContext(sc, prevSecurityContext, modifyCurrentExecutionContext, ref stackMark);
325 [HandleProcessCorruptedStateExceptions]
326 internal static SecurityContextSwitcher SetSecurityContext(SecurityContext sc, Reader prevSecurityContext,
bool modifyCurrentExecutionContext, ref StackCrawlMark stackMark)
328 SecurityContextDisableFlow disableFlow = sc._disableFlow;
329 sc._disableFlow = SecurityContextDisableFlow.Nothing;
330 SecurityContextSwitcher result =
default(SecurityContextSwitcher);
332 result.prevSC = prevSecurityContext;
333 if (modifyCurrentExecutionContext)
343 if (!_LegacyImpersonationPolicy)
345 if (sc.WindowsIdentity !=
null)
347 result.wic = sc.WindowsIdentity.Impersonate(ref stackMark);
349 else if ((disableFlow & SecurityContextDisableFlow.WI) == SecurityContextDisableFlow.Nothing && prevSecurityContext.WindowsIdentity !=
null)
354 result.cssw =
CompressedStack.SetCompressedStack(sc.CompressedStack, prevSecurityContext.CompressedStack);
359 result.UndoNoThrow();
369 [SecuritySafeCritical]
377 securityContext.isNewCapture =
true;
378 securityContext._disableFlow = _disableFlow;
383 if (_compressedStack !=
null)
385 securityContext._compressedStack = _compressedStack.
CreateCopy();
387 return securityContext;
390 [SecuritySafeCritical]
394 securityContext._disableFlow = _disableFlow;
399 if (_compressedStack !=
null)
401 securityContext._compressedStack = _compressedStack.
CreateCopy();
403 return securityContext;
409 [SecuritySafeCritical]
416 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
418 if (securityContext ==
null)
420 securityContext = CreateFullTrustSecurityContext();
422 return securityContext;
429 if (currThreadEC.SecurityContext.IsFlowSuppressed(SecurityContextDisableFlow.All))
433 if (CurrentlyInDefaultFTSecurityContext(currThreadEC))
437 return CaptureCore(currThreadEC, ref stackMark);
441 private static SecurityContext CaptureCore(
ExecutionContext.
Reader currThreadEC, ref StackCrawlMark stackMark)
443 SecurityContext securityContext =
new SecurityContext();
444 securityContext.isNewCapture =
true;
448 if (currentWI !=
null)
455 securityContext._disableFlow = SecurityContextDisableFlow.WI;
458 return securityContext;
462 internal static SecurityContext CreateFullTrustSecurityContext()
464 SecurityContext securityContext =
new SecurityContext();
465 securityContext.isNewCapture =
true;
468 securityContext._disableFlow = SecurityContextDisableFlow.WI;
471 return securityContext;
478 return GetCurrentWI(threadEC, _alwaysFlowImpersonationPolicy);
485 if (cachedAlwaysFlowImpersonationPolicy)
489 return threadEC.SecurityContext.WindowsIdentity;
495 if (cachedAlwaysFlowImpersonationPolicy || prevEC.SecurityContext.WindowsIdentity != targetWI)
497 RestoreCurrentWIInternal(targetWI);
504 int num = Win32.RevertToSelf();
507 Environment.FailFast(Win32Native.GetMessage(num));
509 if (targetWI ==
null)
513 SafeAccessTokenHandle accessToken = targetWI.
AccessToken;
514 if (accessToken !=
null && !accessToken.IsInvalid)
516 num = Win32.ImpersonateLoggedOnUser(accessToken);
519 Environment.FailFast(Win32Native.GetMessage(num));
525 internal bool IsDefaultFTSecurityContext()
542 if (IsDefaultThreadSecurityInfo())
544 return GetCurrentWI(threadEC) ==
null;
550 [SecuritySafeCritical]
551 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
552 internal static extern WindowsImpersonationFlowMode GetImpersonationFlowMode();
556 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
557 internal static extern bool IsDefaultThreadSecurityInfo();
static Thread CurrentThread
Gets the currently running thread.
static void RestoreFlow()
Restores the flow of the security context across asynchronous threads.
delegate void TryCode(object userData)
Represents a delegate to code that should be run in a try block..
static bool IsWindowsIdentityFlowSuppressed()
Determines whether the flow of the Windows identity portion of the current security context has been ...
delegate void ContextCallback(object state)
Represents a method to be called within a new context.
static void ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, object userData)
Executes code using a T:System.Delegate while using another T:System.Delegate to execute additional c...
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
static AsyncFlowControl SuppressFlow()
Suppresses the flow of the security context across asynchronous threads.
Encapsulates and propagates all security-related data for execution contexts transferred across threa...
static AsyncFlowControl SuppressFlowWindowsIdentity()
Suppresses the flow of the Windows identity portion of the current security context across asynchrono...
Provides the functionality to restore the migration, or flow, of the execution context between thread...
Cer
Specifies a method's behavior when called within a constrained execution region.
SecurityContext CreateCopy()
Creates a copy of the current security context.
Represents a Windows user.
static CompressedStack GetCompressedStack()
Gets the compressed stack for the current thread.
Provides information about, and means to manipulate, the current environment and platform....
Manages the execution context for the current thread. This class cannot be inherited.
static SecurityContext Capture()
Captures the security context for the current thread.
static void PrepareConstrainedRegions()
Designates a body of code as a constrained execution region (CER).
static bool IsFlowSuppressed()
Determines whether the flow of the security context has been suppressed.
SafeAccessTokenHandle AccessToken
Gets this T:Microsoft.Win32.SafeHandles.SafeAccessTokenHandle for this T:System.Security....
MethodImplOptions
Defines the details of how a method is implemented.
static void Run(SecurityContext securityContext, ContextCallback callback, object state)
Runs the specified method in the specified security context on the current thread.
delegate void CleanupCode(object userData, bool exceptionThrown)
Represents a method to run when an exception occurs.
Provides methods for setting and capturing the compressed stack on the current thread....
The exception that is thrown when a method call is invalid for the object's current state.
Consistency
Specifies a reliability contract.
void Dispose()
Releases all resources used by the current instance of the T:System.Security.SecurityContext class.
TokenAccessLevels
Defines the privileges of the user account associated with the access token.
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.Security.Principal.WindowsIdentity and optional...
CompressedStack CreateCopy()
Creates a copy of the current compressed stack.
Provides a set of static methods and properties that provide support for compilers....
Creates and controls a thread, sets its priority, and gets its status.