aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.Updater/IdentityUtils.cs
diff options
context:
space:
mode:
authorMirta <mirta@twine-s.com>2020-12-30 16:39:52 +0200
committerMirta <mirta@twine-s.com>2020-12-30 16:39:52 +0200
commit00a491d93733d4625ad329b2ba8237f445364b3f (patch)
tree4b24c6fa78d7648f4bb7cefafa464bb0b063fec4 /Software/Visual_Studio/PPC/Tango.PPC.Updater/IdentityUtils.cs
parent124ad4150f80c6846fdee41dbbda9848c105f6e5 (diff)
downloadTango-00a491d9.tar.gz
Tango-00a491d9.zip
merge
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Updater/IdentityUtils.cs')
-rw-r--r--Software/Visual_Studio/PPC/Tango.PPC.Updater/IdentityUtils.cs118
1 files changed, 0 insertions, 118 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Updater/IdentityUtils.cs b/Software/Visual_Studio/PPC/Tango.PPC.Updater/IdentityUtils.cs
deleted file mode 100644
index 3a43be6f9..000000000
--- a/Software/Visual_Studio/PPC/Tango.PPC.Updater/IdentityUtils.cs
+++ /dev/null
@@ -1,118 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Runtime.InteropServices;
-using System.Security.Principal;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Tango.PPC.Updater
-{
- public static class IdentityUtils
- {
- [DllImport("advapi32.dll", SetLastError = true)]
- static extern bool GetTokenInformation(IntPtr tokenHandle, TokenInformationClass tokenInformationClass, IntPtr tokenInformation, int tokenInformationLength, out int returnLength);
-
- /// <summary>
- /// Passed to <see cref="GetTokenInformation"/> to specify what
- /// information about the token to return.
- /// </summary>
- enum TokenInformationClass
- {
- TokenUser = 1,
- TokenGroups,
- TokenPrivileges,
- TokenOwner,
- TokenPrimaryGroup,
- TokenDefaultDacl,
- TokenSource,
- TokenType,
- TokenImpersonationLevel,
- TokenStatistics,
- TokenRestrictedSids,
- TokenSessionId,
- TokenGroupsAndPrivileges,
- TokenSessionReference,
- TokenSandBoxInert,
- TokenAuditPolicy,
- TokenOrigin,
- TokenElevationType,
- TokenLinkedToken,
- TokenElevation,
- TokenHasRestrictions,
- TokenAccessInformation,
- TokenVirtualizationAllowed,
- TokenVirtualizationEnabled,
- TokenIntegrityLevel,
- TokenUiAccess,
- TokenMandatoryPolicy,
- TokenLogonSid,
- MaxTokenInfoClass
- }
-
- /// <summary>
- /// The elevation type for a user token.
- /// </summary>
- enum TokenElevationType
- {
- TokenElevationTypeDefault = 1,
- TokenElevationTypeFull,
- TokenElevationTypeLimited
- }
-
- public static bool IsElevated()
- {
- var identity = WindowsIdentity.GetCurrent();
- if (identity == null) throw new InvalidOperationException("Couldn't get the current user identity");
- var principal = new WindowsPrincipal(identity);
-
- // Check if this user has the Administrator role. If they do, return immediately.
- // If UAC is on, and the process is not elevated, then this will actually return false.
- //if (principal.IsInRole(WindowsBuiltInRole.Administrator)) return true;
-
- //// If we're not running in Vista onwards, we don't have to worry about checking for UAC.
- //if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version.Major < 6)
- //{
- // // Operating system does not support UAC; skipping elevation check.
- // return false;
- //}
-
- int tokenInfLength = Marshal.SizeOf(typeof(int));
- IntPtr tokenInformation = Marshal.AllocHGlobal(tokenInfLength);
-
- try
- {
- var token = identity.Token;
- var result = GetTokenInformation(token, TokenInformationClass.TokenElevationType, tokenInformation, tokenInfLength, out tokenInfLength);
-
- if (!result)
- {
- var exception = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
- throw new InvalidOperationException("Couldn't get token information", exception);
- }
-
- var elevationType = (TokenElevationType)Marshal.ReadInt32(tokenInformation);
-
- switch (elevationType)
- {
- case TokenElevationType.TokenElevationTypeDefault:
- // TokenElevationTypeDefault - User is not using a split token, so they cannot elevate.
- return false;
- case TokenElevationType.TokenElevationTypeFull:
- // TokenElevationTypeFull - User has a split token, and the process is running elevated. Assuming they're an administrator.
- return true;
- case TokenElevationType.TokenElevationTypeLimited:
- // TokenElevationTypeLimited - User has a split token, but the process is not running elevated. Assuming they're an administrator.
- return false;
- default:
- // Unknown token elevation type.
- return false;
- }
- }
- finally
- {
- if (tokenInformation != IntPtr.Zero) Marshal.FreeHGlobal(tokenInformation);
- }
- }
- }
-}