这个问题就是我要问的问题,但答案并未提供有关_token如何派生的详细信息.它似乎只是使用WindowsIdentity.GetCurrent().Token所以没有冒充发生.
我可以在.NET中模拟其他Active Directory域上的用户吗?
接下来的问题有相互矛盾的答案,接受的答案是"我开始怀疑我的问题出在其他地方." 没用.
下一个问题似乎暗示它是不可能的,但它涉及2个域,所以我不确定它是否相关.
我真正的问题是:
我到目前为止尝试的是,使用http://msdn.microsoft.com/en-us/library/chf6fbt4%28v=VS.80%29.aspx中的代码
bool returnValue = LogonUser(user, domain, password,
LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
// after this point, returnValue = false
Run Code Online (Sandbox Code Playgroud)
Win32错误是
登录失败:未知的用户名或密码错误
我想定期在Windows服务的指定用户帐户下运行任意.NET exe.
到目前为止,我已经让我的Windows服务运行逻辑来决定目标进程是什么,以及何时运行它.目标进程以下列方式启动:
在第一次出现这种情况,目标进程执行罚款,然后正常关闭.然而,每次后续时间,一旦目标进程启动,它就会抛出错误"应用程序无法正常运行(0xc0000142)".重新启动Windows服务将允许进程再次成功运行(第一次执行).
当然,目标是每次都成功执行目标进程.
关于上面的步骤2:要以不同的用户身份运行进程.NET调用win32函数CreateProcessWithLogonW.此函数需要一个窗口句柄来记录指定的用户.由于Windows服务未在交互模式下运行,因此它没有窗口句柄.这个中间过程解决了这个问题,因为它有一个可以传递给目标进程的窗口句柄.
请不要使用psexec或windows任务规划器的建议.我已经接受了我的生活,包括以上述方式解决问题.
我想在另一个用户名的凭据下启动一个进程.这就是我现在拥有的:
/// <summary>
/// Do actions under another username's credentials
/// </summary>
/// <param name="username">Username to inpersonate</param>
/// <param name="domain">Domain/Machine</param>
/// <param name="password">Password </param>
public static void Action(string username,string domain, string password )
{
try
{
if (LogonUser(username, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref hToken))
{
if (DuplicateToken(hToken, 2, ref hTokenDuplicate))
{
WindowsIdentity windowsIdentity = new WindowsIdentity(hTokenDuplicate);
WindowsImpersonationContext impersonationContext = windowsIdentity.Impersonate();
// Check the identity but it could do any other action under given username credentials
try
{
ProcessStartInfo info = …Run Code Online (Sandbox Code Playgroud)