public static void SetTokenInt(TokenHandle hToken, TOKEN_INFORMATION_CLASS infoClass, int value) { bool result = SetTokenInformation(hToken, infoClass, ref value, 4); if (!result) { throw new Win32Exception(); } }
public static extern bool CreateProcessAsUser( TokenHandle hToken, string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes, bool bInheritHandles, CreateProcessFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);
public static TokenHandle DuplicateTokenEx(TokenHandle hExistingToken, TokenAccess desiredAccess, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel, TOKEN_TYPE TokenType) { TokenHandle ret; bool result = DuplicateTokenEx(hExistingToken, (uint)desiredAccess, IntPtr.Zero, ImpersonationLevel, TokenType, out ret); if (!result) { throw new Win32Exception(); } return(ret); }
public static int GetTokenInt(TokenHandle hToken, TOKEN_INFORMATION_CLASS infoClass) { int size; int value = 0; bool result = GetTokenInformation(hToken, infoClass, ref value, 4, out size); if (!result) { throw new Win32Exception(); } return(value); }
public static bool AdjustTokenPrivileges(TokenHandle hToken, bool disableAllPrivileges, PrivilegeState[] newState) { int unused; LUID_AND_ATTRIBUTES[] newState_conv = ConvertTokenPrivileges(newState); bool result = AdjustTokenPrivileges(hToken, disableAllPrivileges, newState_conv, 0, IntPtr.Zero, out unused); if (!result) { throw new Win32Exception(); } return(result); }
public static bool GetTokenElevated(TokenHandle hToken) { int size; TOKEN_ELEVATION te = new TOKEN_ELEVATION(); te.TokenIsElevated = 0; bool result = GetTokenInformation(hToken, TOKEN_INFORMATION_CLASS.TokenElevation, ref te, Marshal.SizeOf(typeof(TOKEN_ELEVATION)), out size); if (!result) { throw new Win32Exception(); } return(te.TokenIsElevated != 0); }
public static TokenHandle GetLinkedToken(TokenHandle hToken) { int size; IntPtr handle = IntPtr.Zero; bool result = GetTokenInformation(hToken, TOKEN_INFORMATION_CLASS.TokenLinkedToken, ref handle, IntPtr.Size, out size); int err = Marshal.GetLastWin32Error(); if (!result) { // this happens if UAC virtualization is not enabled for the token // *that* happens if UAC is disabled for the system, *or* if the user a regular non-admin user. if (err == ERROR_NO_SUCH_LOGON_SESSION) { return(null); } throw new Win32Exception(err); } return(new TokenHandle(handle, true)); }
static extern bool AdjustTokenPrivileges(TokenHandle hToken, bool disableAllPrivileges, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(TokenPrivilegesMarshaler))] LUID_AND_ATTRIBUTES[] NewState, int BufferLength, IntPtr PreviousState, out int returnLength);
public static SECURITY_IMPERSONATION_LEVEL GetTokenImpersonationLevel(TokenHandle hToken) { return((SECURITY_IMPERSONATION_LEVEL)GetTokenInt(hToken, TOKEN_INFORMATION_CLASS.TokenImpersonationLevel)); }
public static TokenType GetTokenType(TokenHandle hToken) { return((TokenType)GetTokenInt(hToken, TOKEN_INFORMATION_CLASS.TokenType)); }
static extern bool SetTokenInformation(TokenHandle TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, [In] ref int TokenInformation, int TokenInformationLength);
static extern bool GetTokenInformation(TokenHandle TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, ref IntPtr TokenInformation, int TokenInformationLength, out int ReturnLength);
extern static bool DuplicateTokenEx(SafeHandle hExistingToken, uint dwDesiredAccess, IntPtr lpTokenAttributes, SECURITY_IMPERSONATION_LEVEL ImpersonationLevel, TOKEN_TYPE TokenType, out TokenHandle phNewToken);
static extern bool OpenProcessToken(IntPtr ProcessHandle, int DesiredAccess, out TokenHandle TokenHandle);