Windows 使用advapi32.dll:LogonUserA()模拟远程计算机';本地用户
我需要能够在远程机器上运行RegLoadKey(),这可能是因为我的机器和远程机器不在同一个域中。如果是,下面的代码工作正常,我可以模拟一个在机器上具有管理员权限的用户。否则,如果我们谈论的是本地用户,根据这次讨论,我发现 …我的计算机上必须有一个具有相同用户名和密码的本地用户。啊。有办法吗Windows 使用advapi32.dll:LogonUserA()模拟远程计算机';本地用户,windows,impersonation,remote-access,Windows,Impersonation,Remote Access,我需要能够在远程机器上运行RegLoadKey(),这可能是因为我的机器和远程机器不在同一个域中。如果是,下面的代码工作正常,我可以模拟一个在机器上具有管理员权限的用户。否则,如果我们谈论的是本地用户,根据这次讨论,我发现 …我的计算机上必须有一个具有相同用户名和密码的本地用户。啊。有办法吗 using System.Runtime.InteropServices; using System.Security.Principal; [DllImport("advapi32.dll")] pu
using System.Runtime.InteropServices;
using System.Security.Principal;
[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
public WindowsImpersonationContext WearDrag(string Username, string Password, string DomainOrMachine)
{
WindowsImpersonationContext impersonationContext;
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(Username, DomainOrMachine, 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 impersonationContext;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return null;
}
以下是我在不必定义本地用户的情况下使用的内容:
const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
const int LOGON32_PROVIDER_DEFAULT = 0;
bool isSuccess = LogonUser(username, domain, password,
LOGON32_LOGON_NEW_CREDENTIALS,
LOGON32_PROVIDER_DEFAULT, ref token);
之后:
WindowsIdentity newIdentity = new WindowsIdentity(token);
WindowsImpersonationContext impersonatedUser = newIdentity.Impersonate();
不过我没有复制把手
另一个观察-我不使用LogonUserA,我只是使用LogonUser。好吧,现在它变得很奇怪:它接受任何用户名和密码,然后返回成功!顺便说一句,在你的头像上的比赛-这是摩纳哥,车手看起来像艾默生·菲蒂帕迪。我的防火墙阻止了巧克力饼,所以你可以得到更多的选票我相信你需要使用LOGON32\u LOGON\u NETWORK=3而不是LOGON32\u LOGON\u NEW\u凭据来强制域身份验证。对我来说,这个博客解决了这个问题: