在另一个用户上下文中执行code [英] Execute code in another users context
问题描述
我有,需要以管理员身份运行清单的应用程序,但应用程序的一部分是映射使用WNetAddConnection2一个驱动器,我认为要求将其在正常范围内的用户由于凭据等运行是否有办法而无需创建一个单独的进程来执行code在正常用户上下文该位。
I have an app with a manifest that requires running as administrator, but part of the app is to map a drive using WNetAddConnection2 which I believe requires it to be run in the normal user context due to credentials etc. Is there a way to execute this bit of code in the normal user context without creating a separate process.
修改
从意见我已经走到这一步,但它不工作。我希望它不要因为我真的不明白挺我应该怎么用这个。也许是最好的,如果我打开一个新的问题?
From the comments I have got this far but it doesnt work. I expected it not to as I dont really understand quite how I should use this. Perhaps it best if I open a new question?
class Program
{
[DllImport("advapi32.DLL")]
public static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
[DllImport("advapi32.DLL")]
public static extern bool RevertToSelf();
static void Main(string[] args)
{
IntPtr phToken = IntPtr.Zero;
ImpersonateLoggedOnUser(phToken);
MapDrives();
RevertToSelf();
}
}
修改
如果当前用户具有管理员权限,那么主流程升高的表现,在code的升高我想,因为这似乎有不同的环境变量来运行在用户的命令不提升的空间等我相信一旦线程开始它不能改变自己,它就需要运行一个新的。
If the current user has admin privileges then the main process is elevated with the manifest, in the code which is elevated I want to run a command in the users non-elevated space as this appears to have different environment variables etc. I believe once a thread is started it cant change itself, it needs to run a new one.
推荐答案
林不知道这是一个办法做到这一点,而无需创建一个新的进程,ImpersonateLoggedOnUser将只从服务工作,我不想提供凭据。
Im not sure this is a way to do this without creating a new process, ImpersonateLoggedOnUser will only work from a service, and I dont want to provide credentials.
纠正我,如果我错了,
这篇关于在另一个用户上下文中执行code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!