每次运行时模拟在成功和失败之间交替进行
本文关键字:之间 失败 运行时 模拟 成功 | 更新日期: 2023-09-27 18:19:21
大家好,
我写了一个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;
}
}
你知道为什么每次运行都失败吗?
对于感兴趣的人来说,错误是处理数据的庄园。请参阅下面我对代码所做的更改来修复这个问题。
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();