1

再会,

我编写了一个 Windows 服务程序来让自己检查某些服务是否在远程机器上运行。该程序每分钟触发一次,然后在服务状态发生变化时提示我。

我使用此代码进行模拟登录

public class Network
{
    public class SoddingNetworkAuth : IDisposable
    {
        [DllImport("advapi32.dll", SetLastError = true)]
        private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);
        [DllImport("kernel32", SetLastError = true)]
        private static extern bool CloseHandle(IntPtr hObject);
        private IntPtr userHandle = IntPtr.Zero;
        private WindowsImpersonationContext impersonationContext;
        public SoddingNetworkAuth(string user, string domain, string password)
        {
            if (!string.IsNullOrEmpty(user))
            {
                // Call LogonUser to get a token for the user  
                bool loggedOn = LogonUser(user, domain, password,
                                9 /*(int)LogonType.LOGON32_LOGON_NEW_CREDENTIALS*/,
                                3 /*(int)LogonProvider.LOGON32_PROVIDER_WINNT50*/,
                out userHandle);
                if (!loggedOn)
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                // Begin impersonating the user  
                impersonationContext = WindowsIdentity.Impersonate(userHandle);
            }
        }
        public void Dispose()
        {
            if (userHandle != IntPtr.Zero)
                CloseHandle(userHandle);
            if (impersonationContext != null)
                impersonationContext.Undo();
        }
    }
}

然后我相应地使用这个结果。

我遇到的问题是,在每秒钟运行一次时,模拟都会失败。因此,如果我有 5 次运行,结果将如下所示:

结果:运行
结果:无法在计算机 [计算机名称] 上打开服务控制管理器
结果:运行
结果:无法在计算机 [计算机名称] 上打开服务控制管理器
结果:运行。

我正在使用域服务帐户登录名(可以更改,如果需要更改登录名)登录有权访问服务控制管理器的机器。

using (new Impersonation.Network.SoddingNetworkAuth(userName, domain, configuration.password))
{
      var serviceController = new ServiceController(configuration.serviceName, configuration.IPaddress);
      if (serviceController.Status == ServiceControllerStatus.Running)
      {
        isRunning = true;
      }
}

关于为什么它在第二次运行时失败的任何想法?

4

1 回答 1

0

对于那些感兴趣的人来说,错误是处理数据的庄园。请参阅下面我对代码所做的更改以解决问题。

try
    {
      if (!string.IsNullOrEmpty(user))
      {
        // Call LogonUser to get a token for the user  
        bool loggedOn = LogonUser(user, domain, password,
                        9 /*(int)LogonType.LOGON32_LOGON_NEW_CREDENTIALS*/,
                        3 /*(int)LogonProvider.LOGON32_PROVIDER_WINNT50*/,
        out userHandle);
        if (!loggedOn)
        {
          if (userHandle != IntPtr.Zero)
          {
            CloseHandle(userHandle);
          }
          throw new Win32Exception(Marshal.GetLastWin32Error());

        }
        // Begin impersonating the user  
        impersonationContext = WindowsIdentity.Impersonate(userHandle);
      }
      if (userHandle != IntPtr.Zero)
      {
        CloseHandle(userHandle);
      }
      disposed = false;
    }
    catch
    {
    }
  }
  public void Dispose()
  {
    try
    {
      //Dispose of unmanaged resources.
      Dispose(true);
      // Suppress finalization.
      GC.SuppressFinalize(this);
    }
    catch
    {
    }

  }
  protected virtual void Dispose(bool Disposing)
  {
    // Check to see if Dispose has already been called.
    try
    {
      if (disposed)
        return;

      if (Disposing)
      {
        if (userHandle != IntPtr.Zero)
        {
          CloseHandle(userHandle);
        }

        if (impersonationContext != null)
        {
          //impersonationContext.Undo();
          impersonationContext.Dispose();
        }
      }
      disposed = true;
    }
    catch
    {
    }

  }

我在主类中调用数据的庄园改为如下:

IntPtr userHandle = IntPtr.Zero;
    bool loggedOn = LogonUser(
userName,
domain,
password,
9,
3,
out userHandle);
    if (!loggedOn)
      throw new Win32Exception(Marshal.GetLastWin32Error());

    WindowsImpersonationContext impersonationContext = WindowsIdentity.Impersonate(userHandle);

          using (var serviceController = new ServiceController(serviceName, IPaddress))
          {
            //code goes here
          }
          serviceController.Dispose();
          if (userHandle != IntPtr.Zero)
            CloseHandle(userHandle);

          if (impersonationContext != null)
            impersonationContext.Undo();
于 2016-01-07T09:08:37.000 回答