internal static int OpenThreadToken(TokenAccessLevels dwDesiredAccess, WinSecurityContext dwOpenAs, out SafeTokenHandle phThreadToken) { int hr = 0; bool openAsSelf = true; if (dwOpenAs == WinSecurityContext.Thread) { openAsSelf = false; } if (!Interop.Advapi32.OpenThreadToken((IntPtr)(-2), dwDesiredAccess, openAsSelf, out phThreadToken)) { if (dwOpenAs == WinSecurityContext.Both) { openAsSelf = false; hr = 0; if (!Interop.Advapi32.OpenThreadToken((IntPtr)(-2), dwDesiredAccess, openAsSelf, out phThreadToken)) { hr = Marshal.GetHRForLastWin32Error(); } } else { hr = Marshal.GetHRForLastWin32Error(); } } if (hr != 0) { phThreadToken = null; } return(hr); }
internal static bool OpenThreadToken(TokenAccessLevels desiredAccess, WinSecurityContext openAs, out SafeAccessTokenHandle tokenHandle) { bool openAsSelf = true; if (openAs == WinSecurityContext.Thread) { openAsSelf = false; } if (OpenThreadToken(Kernel32.GetCurrentThread(), desiredAccess, openAsSelf, out tokenHandle)) { return(true); } if (openAs == WinSecurityContext.Both) { openAsSelf = false; if (OpenThreadToken(Kernel32.GetCurrentThread(), desiredAccess, openAsSelf, out tokenHandle)) { return(true); } } return(false); }
internal static bool OpenThreadToken(TokenAccessLevels desiredAccess, WinSecurityContext openAs, out SafeAccessTokenHandle tokenHandle) { bool openAsSelf = true; if (openAs == WinSecurityContext.Thread) openAsSelf = false; if (OpenThreadToken(GetCurrentThread(), desiredAccess, openAsSelf, out tokenHandle)) return true; if (openAs == WinSecurityContext.Both) { openAsSelf = false; if (OpenThreadToken(GetCurrentThread(), desiredAccess, openAsSelf, out tokenHandle)) return true; } return false; }
/// <summary> /// The OpenThreadToken function opens the access token associated with a thread. /// </summary> /// <param name="dwDesiredAccess"> /// Specifies an access mask that specifies the requested types of access to the access token. /// These requested access types are reconciled against the token's discretionary access control list (DACL) to determine which accesses are granted or denied. /// For a list of access rights for access tokens, see Access Rights for Access-Token Objects. /// </param> /// <param name="dwOpenAs"> /// A flag specifying whether the access check is to be made against the process-level security context, the current security context of the thread calling the OpenThreadToken function, or both. /// </param> /// <param name="phThreadToken"> /// A pointer to a variable that receives the handle to the newly opened access token. /// </param> /// <returns> /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. /// To get extended error information, call GetLastError. /// If the token has the anonymous impersonation level, the token will not be opened and OpenThreadToken sets ERROR_CANT_OPEN_ANONYMOUS as the error. /// </returns> /// <see> /// <cref>https://github.com/dotnet/corefx/blob/master/src/System.Security.AccessControl/src/System/Security/Principal/Win32.cs</cref> /// </see> internal static int OpenThreadToken(TokenAccessLevels dwDesiredAccess, WinSecurityContext dwOpenAs, out SafeTokenHandle phThreadToken) { var openAsSelf = dwOpenAs != WinSecurityContext.Thread; if (OpenThreadToken((IntPtr)(-2), dwDesiredAccess, openAsSelf, out phThreadToken)) { return(0); } if (dwOpenAs != WinSecurityContext.Both) { phThreadToken = new SafeTokenHandle(IntPtr.Zero); return(Marshal.GetHRForLastWin32Error()); } if (OpenThreadToken((IntPtr)(-2), dwDesiredAccess, false, out phThreadToken)) { return(0); } phThreadToken = new SafeTokenHandle(IntPtr.Zero); return(Marshal.GetHRForLastWin32Error()); }
internal static int OpenThreadToken(TokenAccessLevels dwDesiredAccess, WinSecurityContext dwOpenAs, out SafeTokenHandle phThreadToken) { int hr = 0; bool openAsSelf = true; if (dwOpenAs == WinSecurityContext.Thread) openAsSelf = false; if (!Interop.Advapi32.OpenThreadToken((IntPtr)(-2), dwDesiredAccess, openAsSelf, out phThreadToken)) { if (dwOpenAs == WinSecurityContext.Both) { openAsSelf = false; hr = 0; if (!Interop.Advapi32.OpenThreadToken((IntPtr)(-2), dwDesiredAccess, openAsSelf, out phThreadToken)) hr = Marshal.GetHRForLastWin32Error(); } else { hr = Marshal.GetHRForLastWin32Error(); } } if (hr != 0) phThreadToken = null; return hr; }
internal static extern int OpenThreadToken (TokenAccessLevels dwDesiredAccess, WinSecurityContext OpenAs, out SafeTokenHandle phThreadToken);
internal static extern int OpenThreadToken(TokenAccessLevels dwDesiredAccess, WinSecurityContext OpenAs, out SafeAccessTokenHandle phThreadToken);