コード例 #1
0
ファイル: Impersonator.cs プロジェクト: gabrewer/Terminals
 /// <summary>
 /// Begins impersonation with the given credentials.
 /// </summary>
 /// <param name="userName">Name of the user.</param>
 /// <param name="domainName">Name of the domain.</param>
 /// <param name="password">The password. <see cref="System.String"/></param>
 public Impersonator(Credential credential)
 {
     Impersonate(credential, LogonType.LOGON32_LOGON_INTERACTIVE, LogonProvider.LOGON32_PROVIDER_DEFAULT);
 }
コード例 #2
0
ファイル: Impersonator.cs プロジェクト: gabrewer/Terminals
        /// <summary>
        /// Impersonates the specified user account.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="domainName">Name of the domain.</param>
        /// <param name="password">The password. <see cref="System.String"/></param>
        /// <param name="logonType">Type of the logon.</param>
        /// <param name="logonProvider">The logon provider. <see cref="Mit.Sharepoint.WebParts.EventLogQuery.Network.LogonProvider"/></param>
        public void Impersonate(Credential credential, LogonType logonType, LogonProvider logonProvider)
        {
            UndoImpersonation();

               IntPtr logonToken = IntPtr.Zero;
               IntPtr logonTokenDuplicate = IntPtr.Zero;
               try
               {
            // revert to the application pool identity, saving the identity of the current requestor
            _wic = WindowsIdentity.Impersonate(IntPtr.Zero);

            // do logon & impersonate
            if (Win32NativeMethods.LogonUser(credential.UserName,
            credential.Domain,
            credential.Password,
            (int)logonType,
            (int)logonProvider,
            ref logonToken) != 0)
            {
             if (Win32NativeMethods.DuplicateToken(logonToken, (int)ImpersonationLevel.SecurityImpersonation, ref logonTokenDuplicate) != 0)
             {
              var wi = new WindowsIdentity(logonTokenDuplicate);
              wi.Impersonate(); // discard the returned identity context (which is the context of the application pool)
             }
             else
              throw new Win32Exception(Marshal.GetLastWin32Error());
            }
            else
             throw new Win32Exception(Marshal.GetLastWin32Error());
               }
               finally
               {
            if (logonToken != IntPtr.Zero)
             Win32NativeMethods.CloseHandle(logonToken);

            if (logonTokenDuplicate != IntPtr.Zero)
             Win32NativeMethods.CloseHandle(logonTokenDuplicate);
               }
        }
コード例 #3
0
ファイル: Impersonator.cs プロジェクト: gabrewer/Terminals
 /// <summary>
 /// Begins impersonation with the given credentials, Logon type and Logon provider.
 /// </summary>
 /// <param name="userName">Name of the user.</param>
 /// <param name="domainName">Name of the domain.</param>
 /// <param name="password">The password. <see cref="System.String"/></param>
 /// <param name="logonType">Type of the logon.</param>
 /// <param name="logonProvider">The logon provider. <see cref="Mit.Sharepoint.WebParts.EventLogQuery.Network.LogonProvider"/></param>
 public Impersonator(Credential credential, LogonType logonType, LogonProvider logonProvider)
 {
     Impersonate(credential, logonType, logonProvider);
 }
コード例 #4
0
ファイル: MagicPacket.cs プロジェクト: RSchwoerer/Terminals
        /// <summary>
        ///     Send a shutdown command to a (remote) computer.
        /// </summary>
        /// <param name="machineName"> The machinename or ip-address of computer to send shutdown command to. </param>
        /// <param name="shutdownCommand"> Shutdown type command. </param>
        /// <param name="credentials"> Optional network credentials for the (remote) computer. </param>
        /// <returns> 0 if the shutdown was succesfully send, else another integer value. </returns>
        /// <exception cref="ManagementException">An unhandled managed error occured.</exception>
        /// <exception cref="UnauthorizedAccessException">Access was denied.</exception>
        public static Int32 ForceShutdown(String machineName, ShutdownCommands shutdownCommand, Credential credential = null)
        {
            Int32 result = -1;

            ConnectionOptions options = new ConnectionOptions();
            if (credential != null)
            {
                options.EnablePrivileges = true;
                options.Username = credential.UserNameWithDomain;
                options.Password = credential.Password;
            }

            ManagementScope scope = new ManagementScope(String.Format("\\\\{0}\\root\\cimv2", machineName), options);
            scope.Connect();

            SelectQuery query = new SelectQuery("Win32_OperatingSystem");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

            foreach (ManagementObject os in searcher.Get())
            {
                ManagementBaseObject inParams = os.GetMethodParameters("Win32Shutdown");
                inParams["Flags"] = (Int32) shutdownCommand;

                ManagementBaseObject outParams = os.InvokeMethod("Win32Shutdown", inParams, null);
                result = Convert.ToInt32(outParams.Properties["returnValue"].Value);

                return result;
            }

            return result;
        }