/CitiBike.Analysis.Engine/Web/UacProperties.cs

https://gitlab.com/doubledown/CitiBike · C# · 130 lines · 123 code · 7 blank · 0 comment · 11 complexity · 3333dd0a687a2ff7947c4f7e98a414a1 MD5 · raw file

  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Security.Principal;
  5. using Microsoft.Win32;
  6. namespace CitiBike.Analysis.Engine.Web
  7. {
  8. internal static class UacProperties
  9. {
  10. private enum TokenElevationType
  11. {
  12. TokenElevationTypeDefault = 1,
  13. TokenElevationTypeFull,
  14. TokenElevationTypeLimited
  15. }
  16. private enum TokenInformationClass
  17. {
  18. TokenUser = 1,
  19. TokenGroups,
  20. TokenPrivileges,
  21. TokenOwner,
  22. TokenPrimaryGroup,
  23. TokenDefaultDacl,
  24. TokenSource,
  25. TokenType,
  26. TokenImpersonationLevel,
  27. TokenStatistics,
  28. TokenRestrictedSids,
  29. TokenSessionId,
  30. TokenGroupsAndPrivileges,
  31. TokenSessionReference,
  32. TokenSandBoxInert,
  33. TokenAuditPolicy,
  34. TokenOrigin,
  35. TokenElevationType,
  36. TokenLinkedToken,
  37. TokenElevation,
  38. TokenHasRestrictions,
  39. TokenAccessInformation,
  40. TokenVirtualizationAllowed,
  41. TokenVirtualizationEnabled,
  42. TokenIntegrityLevel,
  43. TokenUIAccess,
  44. TokenMandatoryPolicy,
  45. TokenLogonSid,
  46. MaxTokenInfoClass
  47. }
  48. public static bool IsProcessElevated
  49. {
  50. get
  51. {
  52. if (!UacProperties.IsUacEnabled)
  53. {
  54. WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
  55. return principal.IsInRole(WindowsBuiltInRole.Administrator) || principal.IsInRole(0x200); //Domain Administrator
  56. }
  57. IntPtr tokenHandle;
  58. if (!NativeMethods.OpenProcessToken(Process.GetCurrentProcess().Handle, 0x00020000 | 0x0008, out tokenHandle))
  59. {
  60. throw new ApplicationException("Could not get process token. Win32 Error Code: " + Marshal.GetLastWin32Error());
  61. }
  62. try
  63. {
  64. int elevationResultSize = Marshal.SizeOf((int)TokenElevationType.TokenElevationTypeDefault);
  65. IntPtr elevationTypePtr = Marshal.AllocHGlobal(elevationResultSize);
  66. try
  67. {
  68. uint returnedSize;
  69. bool success = NativeMethods.GetTokenInformation(
  70. tokenHandle,
  71. TokenInformationClass.TokenElevationType,
  72. elevationTypePtr,
  73. (uint)elevationResultSize,
  74. out returnedSize
  75. );
  76. if (!success)
  77. {
  78. throw new ApplicationException("Unable to determine the current elevation.");
  79. }
  80. return (TokenElevationType)Marshal.ReadInt32(elevationTypePtr) == TokenElevationType.TokenElevationTypeFull;
  81. }
  82. finally
  83. {
  84. if (elevationTypePtr != IntPtr.Zero)
  85. {
  86. Marshal.FreeHGlobal(elevationTypePtr);
  87. }
  88. }
  89. }
  90. finally
  91. {
  92. if (tokenHandle != IntPtr.Zero)
  93. {
  94. NativeMethods.CloseHandle(tokenHandle);
  95. }
  96. }
  97. }
  98. }
  99. public static bool IsUacEnabled
  100. {
  101. get
  102. {
  103. const string uacRegistryKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
  104. using (RegistryKey uacKey = Registry.LocalMachine.OpenSubKey(uacRegistryKey, false))
  105. {
  106. const string uacRegistryValue = "EnableLUA";
  107. return uacKey != null && object.Equals(uacKey.GetValue(uacRegistryValue), 1);
  108. }
  109. }
  110. }
  111. private static class NativeMethods
  112. {
  113. [DllImport("kernel32.dll", SetLastError = true)]
  114. [return: MarshalAs(UnmanagedType.Bool)]
  115. public static extern bool CloseHandle(IntPtr handle);
  116. [DllImport("advapi32.dll", SetLastError = true)]
  117. public static extern bool GetTokenInformation(IntPtr tokenHandle, TokenInformationClass tokenInformationClass, IntPtr tokenInformation, uint tokenInformationLength, out uint returnLength);
  118. [DllImport("advapi32.dll", SetLastError = true)]
  119. [return: MarshalAs(UnmanagedType.Bool)]
  120. public static extern bool OpenProcessToken(IntPtr processHandle, uint desiredAccess, out IntPtr tokenHandle);
  121. }
  122. }
  123. }