Ejemplo n.º 1
0
        /// <summary>
        /// Logs the specified user on to the local computer
        /// </summary>
        /// <param name="userName">The userName of the user to be impersonated</param>
        /// <param name="domain">The domain of the user</param>
        /// <param name="password">The password of the user</param>
        /// <param name="loadUserProfile">Indicates whether to load user profile or not</param>
        private void LogOn(string userName, string domain, string password, bool loadUserProfile)
        {
            Logger.Instance.WriteMethodEntry(EventIdentifier.LogOnUserLogOn, "Domain: {0}. UserName: {1}. LoadProfile: {2}.", domain, userName, loadUserProfile);

            try
            {
                if (!NativeMethods.LogonUser(userName, domain, password, this.logonType, this.logonProvider, out this.primaryToken))
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }

                if (!loadUserProfile)
                {
                    return;
                }

                NativeMethods.PROFILEINFO profileInfo = new NativeMethods.PROFILEINFO();
                profileInfo.dwSize = Marshal.SizeOf(profileInfo);
                profileInfo.lpUserName = userName;
                profileInfo.lpServerName = domain;
                profileInfo.dwFlags = NativeMethods.PI_NOUI;

                if (!NativeMethods.LoadUserProfile(this.Token, ref profileInfo) || profileInfo.hProfile == IntPtr.Zero)
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }

                this.profileHandle = new SafeRegistryHandle(profileInfo.hProfile, true);
            }
            finally
            {
                Logger.Instance.WriteMethodExit(EventIdentifier.LogOnUserLogOn, "Domain: {0}. UserName: {1}. LoadProfile: {2}.", domain, userName, loadUserProfile);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Releases all resources used by the class and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        private void Dispose(bool disposing)
        {
            Logger.Instance.WriteMethodEntry(EventIdentifier.LogOnUserDispose, "Disposing: {0}.", disposing);

            try
            {
                if (!this.disposed)
                {
                    if (disposing)
                    {
                        if (this.profileHandle != null)
                        {
                            NativeMethods.UnloadUserProfile(this.Token, this.UserProfileHandle);
                            this.profileHandle.Dispose();
                            this.profileHandle = null;
                        }

                        if (this.primaryToken != null)
                        {
                            this.primaryToken.Dispose();
                            this.primaryToken = null;
                        }
                    }
                }

                this.disposed = true;
            }
            finally
            {
                Logger.Instance.WriteMethodExit(EventIdentifier.LogOnUserDispose, "Disposing: {0}.", disposing);
            }
        }