public HostedImpersonationContext() { if (ServiceHostingEnvironment.AspNetCompatibilityEnabled) { int error; bool isSuccess = SafeNativeMethods.OpenCurrentThreadTokenCritical(TokenAccessLevels.Query | TokenAccessLevels.Impersonate, true, out tokenHandle, out error); if (isSuccess) { isImpersonated = true; Interlocked.Increment(ref refCount); } else { CloseInvalidOutSafeHandleCritical(tokenHandle); tokenHandle = null; if (error != SafeNativeMethods.ERROR_NO_TOKEN) { throw FxTrace.Exception.AsError(new Win32Exception(error, SR.Hosting_ImpersonationFailed)); } } } }
private static extern bool OpenThreadTokenCritical([In] IntPtr ThreadHandle, [In] TokenAccessLevels DesiredAccess, [In] bool OpenAsSelf, out SafeCloseHandleCritical TokenHandle);
internal static bool OpenCurrentThreadTokenCritical(TokenAccessLevels desiredAccess, bool openAsSelf, out SafeCloseHandleCritical tokenHandle, out int error) { bool flag = OpenThreadTokenCritical(GetCurrentThread(), desiredAccess, openAsSelf, out tokenHandle); error = Marshal.GetLastWin32Error(); return flag; }
internal static bool OpenCurrentThreadTokenCritical(TokenAccessLevels desiredAccess, bool openAsSelf, out SafeCloseHandleCritical tokenHandle, out int error) { bool flag = OpenThreadTokenCritical(GetCurrentThread(), desiredAccess, openAsSelf, out tokenHandle); error = Marshal.GetLastWin32Error(); return(flag); }
public void Release() { if (IsImpersonated) { Fx.Assert(tokenHandle != null, "The token handle is incorrectly released earlier."); int currentCount = Interlocked.Decrement(ref refCount); if (currentCount == 0) { lock (tokenHandle) { tokenHandle.Close(); tokenHandle = null; } } } }