Beispiel #1
0
 public static SafeNativeHandle GetTokenLinkedToken(SafeNativeHandle hToken)
 {
     using (SafeMemoryBuffer tokenInfo = GetTokenInformation(hToken,
                                                             NativeHelpers.TokenInformationClass.TokenLinkedToken))
     {
         return(new SafeNativeHandle(Marshal.ReadIntPtr(tokenInfo.DangerousGetHandle())));
     }
 }
Beispiel #2
0
 public static TokenElevationType GetTokenElevationType(SafeNativeHandle hToken)
 {
     using (SafeMemoryBuffer tokenInfo = GetTokenInformation(hToken,
                                                             NativeHelpers.TokenInformationClass.TokenElevationType))
     {
         return((TokenElevationType)Marshal.ReadInt32(tokenInfo.DangerousGetHandle()));
     }
 }
Beispiel #3
0
 public static TokenStatistics GetTokenStatistics(SafeNativeHandle hToken)
 {
     using (SafeMemoryBuffer tokenInfo = GetTokenInformation(hToken,
                                                             NativeHelpers.TokenInformationClass.TokenStatistics))
     {
         TokenStatistics tokenStats = (TokenStatistics)Marshal.PtrToStructure(
             tokenInfo.DangerousGetHandle(),
             typeof(TokenStatistics));
         return(tokenStats);
     }
 }
Beispiel #4
0
 public static SecurityIdentifier GetTokenUser(SafeNativeHandle hToken)
 {
     using (SafeMemoryBuffer tokenInfo = GetTokenInformation(hToken,
                                                             NativeHelpers.TokenInformationClass.TokenUser))
     {
         NativeHelpers.TOKEN_USER tokenUser = (NativeHelpers.TOKEN_USER)Marshal.PtrToStructure(
             tokenInfo.DangerousGetHandle(),
             typeof(NativeHelpers.TOKEN_USER));
         return(new SecurityIdentifier(tokenUser.User.Sid));
     }
 }
Beispiel #5
0
        public static List <PrivilegeInfo> GetTokenPrivileges(SafeNativeHandle hToken)
        {
            using (SafeMemoryBuffer tokenInfo = GetTokenInformation(hToken,
                                                                    NativeHelpers.TokenInformationClass.TokenPrivileges))
            {
                NativeHelpers.TOKEN_PRIVILEGES tokenPrivs = (NativeHelpers.TOKEN_PRIVILEGES)Marshal.PtrToStructure(
                    tokenInfo.DangerousGetHandle(),
                    typeof(NativeHelpers.TOKEN_PRIVILEGES));

                NativeHelpers.LUID_AND_ATTRIBUTES[] luidAttrs =
                    new NativeHelpers.LUID_AND_ATTRIBUTES[tokenPrivs.PrivilegeCount];
                PtrToStructureArray(luidAttrs, IntPtr.Add(tokenInfo.DangerousGetHandle(),
                                                          Marshal.SizeOf(tokenPrivs.PrivilegeCount)));

                return(luidAttrs.Select(la => new PrivilegeInfo(la)).ToList());
            }
        }
Beispiel #6
0
        private static SafeMemoryBuffer GetTokenInformation(SafeNativeHandle hToken,
                                                            NativeHelpers.TokenInformationClass infoClass)
        {
            UInt32 tokenLength;
            bool   res = NativeMethods.GetTokenInformation(hToken, infoClass, new SafeMemoryBuffer(IntPtr.Zero), 0,
                                                           out tokenLength);
            int errCode = Marshal.GetLastWin32Error();

            if (!res && errCode != 24 && errCode != 122)  // ERROR_INSUFFICIENT_BUFFER, ERROR_BAD_LENGTH
            {
                throw new Win32Exception(errCode, String.Format("GetTokenInformation({0}) failed to get buffer length",
                                                                infoClass.ToString()));
            }

            SafeMemoryBuffer tokenInfo = new SafeMemoryBuffer((int)tokenLength);

            if (!NativeMethods.GetTokenInformation(hToken, infoClass, tokenInfo, tokenLength, out tokenLength))
            {
                throw new Win32Exception(String.Format("GetTokenInformation({0}) failed", infoClass.ToString()));
            }

            return(tokenInfo);
        }
Beispiel #7
0
 public static extern bool GetTokenInformation(
     SafeNativeHandle TokenHandle,
     NativeHelpers.TokenInformationClass TokenInformationClass,
     SafeMemoryBuffer TokenInformation,
     UInt32 TokenInformationLength,
     out UInt32 ReturnLength);