美好的一天,
我编写了一个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.
我正在使用域服务帐户登录(如果需要更改登录,可以更改)登录到有权访问服务控制管理器的计算机。
using (new Impersonation.Network.SoddingNetworkAuth(userName, domain, configuration.password))
{
var serviceController = new ServiceController(configuration.serviceName, configuration.IPaddress);
if (serviceController.Status == ServiceControllerStatus.Running)
{
isRunning = true;
}
}
关于为什么每次运行都失败的想法?
答案 0 :(得分:0)
对于那些感兴趣的人来说,rror是处理数据的庄园。请参阅下面我对代码所做的更改以解决问题。
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();