Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 模拟在每次运行的成功和失败之间交替进行_C#_Active Directory_Impersonation_Service Accounts_Service Control Manager - Fatal编程技术网

C# 模拟在每次运行的成功和失败之间交替进行

C# 模拟在每次运行的成功和失败之间交替进行,c#,active-directory,impersonation,service-accounts,service-control-manager,C#,Active Directory,Impersonation,Service Accounts,Service Control Manager,你好 我编写了一个Windows服务程序,让自己检查某些服务是否在远程机器上运行。 程序每分钟触发一次,然后在服务状态更改时提示我 我使用此代码进行模拟登录 public class Network { public class SoddingNetworkAuth : IDisposable { [DllImport("advapi32.dll", SetLastError = true)] private static extern bool

你好

我编写了一个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次运行,结果如下:

result:Running result:Cannot open Service Control Manager on computer [Computer Name] result:Running result:Cannot open Service Control Manager on computer [Computer Name] result:Running.
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();

关于为什么它每运行一秒钟就会失败,你有什么想法吗?

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

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
    {
    }

  }
我在主类中调用数据的庄园更改为:

result:Running result:Cannot open Service Control Manager on computer [Computer Name] result:Running result:Cannot open Service Control Manager on computer [Computer Name] result:Running.
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();

这可能是一个整天都在运行的程序,还是每分钟启动一个新实例?能否将创建SoddingNetworkAuth连接的代码添加到服务控制管理器?它是一个windows服务,每分钟都会触发它运行。