我正在寻找一种使用 C# 代码启动/停止驻留在远程计算机中的 Windows 服务的方法,并找到了以下代码示例。这对我来说可以。它是使用模拟技术编码的,这显然要求两台机器(比如说 A 和 B)都有一个具有相同用户名 + 密码组合的用户帐户。
int LOGON32_LOGON_INTERACTIVE = 2;
int LOGON32_PROVIDER_DEFAULT = 0;
private bool impersonateValidUser(String userName, String machineName, String passWord)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, machineName, passWord,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
{
CloseHandle(token);
}
if (tokenDuplicate != IntPtr.Zero)
{
CloseHandle(tokenDuplicate);
}
return false;
}
现在我需要知道以下问题的答案,如果有人能帮助我,我将不胜感激。
对代码的一般解释。
为什么两台机器都必须拥有具有相同用户名+密码组合的用户帐户?
为什么两个用户帐户(管理员或非管理员)的权限无关紧要?
先感谢您。