コード例 #1
0
 public void Unload()
 {
     // See https://msdn.microsoft.com/en-us/library/windows/desktop/bb762282(v=vs.85).aspx
     // This function closes the registry handle for the user profile too
     Win32Helper.Invoke(() => UnloadUserProfile(token.Handle, userProfile),
                        $"Failed to unload user profile for user '{token.Username}'");
 }
コード例 #2
0
        public static void GrantAccessToWindowStationAndDesktop(string username, string?domainName = null)
        {
            var       hWindowStation         = Win32Helper.Invoke(() => GetProcessWindowStation(), "Failed to get a handle to the current window station for this process");
            const int windowStationAllAccess = 0x000f037f;

            GrantAccess(username, domainName, hWindowStation, windowStationAllAccess);
            var       hDesktop = Win32Helper.Invoke(() => GetThreadDesktop(GetCurrentThreadId()), "Failed to the a handle to the desktop for the current thread");
            const int desktopRightsAllAccess = 0x000f01ff;

            GrantAccess(username, domainName, hDesktop, desktopRightsAllAccess);
        }
コード例 #3
0
        public static UserProfile Load(AccessToken token)
        {
            var userProfile = new PROFILEINFO
            {
                lpUserName = token.Username
            };

            userProfile.dwSize = Marshal.SizeOf(userProfile);

            // See https://msdn.microsoft.com/en-us/library/windows/desktop/bb762281(v=vs.85).aspx
            Win32Helper.Invoke(() => LoadUserProfile(token.Handle, ref userProfile),
                               $"Failed to load user profile for user '{token.Username}'");

            return(new UserProfile(token, new SafeRegistryHandle(userProfile.hProfile, false)));
        }
コード例 #4
0
        public static AccessToken Logon(string username,
                                        string password,
                                        string domain               = ".",
                                        LogonType logonType         = LogonType.Network,
                                        LogonProvider logonProvider = LogonProvider.Default)
        {
            // See https://msdn.microsoft.com/en-us/library/windows/desktop/aa378184(v=vs.85).aspx
            var hToken = IntPtr.Zero;

            Win32Helper.Invoke(() => LogonUser(username,
                                               domain,
                                               password,
                                               LogonType.Network,
                                               LogonProvider.Default,
                                               out hToken),
                               $"Logon failed for the user '{username}'");

            return(new AccessToken(username, new SafeAccessTokenHandle(hToken)));
        }
コード例 #5
0
        internal static Dictionary <string, string> GetEnvironmentVariablesForUser(AccessToken token, bool inheritFromCurrentProcess)
        {
            var env = IntPtr.Zero;

            // See https://msdn.microsoft.com/en-us/library/windows/desktop/bb762270(v=vs.85).aspx
            Win32Helper.Invoke(() => CreateEnvironmentBlock(out env, token.Handle, inheritFromCurrentProcess),
                               $"Failed to load the environment variables for the user '{token.Username}'");

            var userEnvironment = new Dictionary <string, string>();

            try
            {
                var testData = new StringBuilder();
                unsafe
                {
                    // The environment block is an array of null-terminated Unicode strings.
                    // Key and Value are separated by =
                    // The list ends with two nulls (\0\0).
                    var start   = (short *)env.ToPointer();
                    var done    = false;
                    var current = start;
                    while (!done)
                    {
                        if (testData.Length > 0 && *current == 0 && current != start)
                        {
                            var data  = testData.ToString();
                            var index = data.IndexOf('=');
                            if (index == -1)
                            {
                                userEnvironment.Add(data, "");
                            }
                            else if (index == data.Length - 1)
                            {
                                userEnvironment.Add(data.Substring(0, index), "");
                            }
                            else
                            {
                                userEnvironment.Add(data.Substring(0, index), data.Substring(index + 1));
                            }
                            testData.Length = 0;
                        }

                        if (*current == 0 && current != start && *(current - 1) == 0)
                        {
                            done = true;
                        }
                        if (*current != 0)
                        {
                            testData.Append((char)*current);
                        }
                        current++;
                    }
                }
            }
            finally
            {
                // See https://msdn.microsoft.com/en-us/library/windows/desktop/bb762274(v=vs.85).aspx
                Win32Helper.Invoke(() => DestroyEnvironmentBlock(env),
                                   $"Failed to destroy the environment variables structure for user '{token.Username}'");
            }

            return(userEnvironment);
        }