public TokenProvider EnablePrivilege(Privilege securityEntity, bool throwOnFailure) { // todo: rewrite to use just 1 api call, handle exceptions, var locallyUniqueIdentifier = new NativeMethods.LUID(); if (!NativeMethods.LookupPrivilegeValue(null, securityEntity.ToString(), ref locallyUniqueIdentifier)) { throw new Win32Exception(); } var TOKEN_PRIVILEGES = new NativeMethods.TOKEN_PRIVILEGES(); TOKEN_PRIVILEGES.PrivilegeCount = 1; TOKEN_PRIVILEGES.Attributes = NativeMethods.SE_PRIVILEGE_ENABLED; TOKEN_PRIVILEGES.Luid = locallyUniqueIdentifier; if (!NativeMethods.AdjustTokenPrivileges(Token.DangerousGetHandle(), false, ref TOKEN_PRIVILEGES, 1024, IntPtr.Zero, IntPtr.Zero)) { if (throwOnFailure) { throw new Win32Exception(); } } return(this); }
public static void DisableAllPrivileges(IntPtr tokenHandle) { var TOKEN_PRIVILEGES = new NativeMethods.TOKEN_PRIVILEGES(); if (!NativeMethods.AdjustTokenPrivileges(tokenHandle, true, ref TOKEN_PRIVILEGES, 1024, IntPtr.Zero, IntPtr.Zero)) { throw new Win32Exception(); } }