Example #1
0
            /// <summary>
            /// The function checks whether the primary access token of the process belongs
            /// to user account that is a member of the local Administrators group, even if
            /// it currently is not elevated.
            /// </summary>
            /// <returns>
            /// Returns true if the primary access token of the process belongs to user
            /// account that is a member of the local Administrators group. Returns false
            /// if the token does not.
            /// </returns>
            /// <exception cref="System.ComponentModel.Win32Exception">
            /// When any native Windows API call fails, the function throws a Win32Exception
            /// with the last error code.
            /// </exception>
            public static bool IsUserInAdminGroup()
            {
                bool            fInAdminGroup = false;
                SafeTokenHandle hTokenToCheck = null;

                // Open the access token of the current process for query and duplicate.
                SafeTokenHandle hToken = SafeTokenHandle.FromCurrentProcess(AccessTypes.TokenQuery | AccessTypes.TokenDuplicate);

                // Determine whether system is running Windows Vista or later operating
                // systems (major version >= 6) because they support linked tokens, but
                // previous versions (major version < 6) do not.
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    // Running Windows Vista or later (major version >= 6).
                    // Determine token type: limited, elevated, or default.

                    // Marshal the TOKEN_ELEVATION_TYPE enum from native to .NET.
                    TOKEN_ELEVATION_TYPE elevType = hToken.GetInfo <TOKEN_ELEVATION_TYPE>(TOKEN_INFORMATION_CLASS.TokenElevationType);

                    // If limited, get the linked elevated token for further check.
                    if (elevType == TOKEN_ELEVATION_TYPE.Limited)
                    {
                        // Marshal the linked token value from native to .NET.
                        IntPtr hLinkedToken = hToken.GetInfo <IntPtr>(TOKEN_INFORMATION_CLASS.TokenLinkedToken);
                        hTokenToCheck = new SafeTokenHandle(hLinkedToken);
                    }
                }

                // CheckTokenMembership requires an impersonation token. If we just got
                // a linked token, it already is an impersonation token.  If we did not
                // get a linked token, duplicate the original into an impersonation
                // token for CheckTokenMembership.
                if (hTokenToCheck == null)
                {
                    if (!NativeMethods.DuplicateToken(hToken, SECURITY_IMPERSONATION_LEVEL.Identification, out hTokenToCheck))
                    {
                        throw new Win32Exception();
                    }
                }

                // Check if the token to be checked contains admin SID.
                WindowsIdentity  id        = new WindowsIdentity(hTokenToCheck.DangerousGetHandle());
                WindowsPrincipal principal = new WindowsPrincipal(id);

                fInAdminGroup = principal.IsInRole(WindowsBuiltInRole.Administrator);

                return(fInAdminGroup);
            }
Example #2
0
 public static IEnumerable <LUID_AND_ATTRIBUTES> GetPrivileges(this SafeTokenHandle hObj) =>
 hObj.GetInfo <PTOKEN_PRIVILEGES>(TOKEN_INFORMATION_CLASS.TokenPrivileges).Privileges;