C# 是否可以使用没有windows服务的服务帐户(域)在其他用户(模拟)下运行代码?

C# 是否可以使用没有windows服务的服务帐户(域)在其他用户(模拟)下运行代码?,c#,wpf,impersonation,C#,Wpf,Impersonation,我们希望在服务帐户的上下文中运行进程。 WPF应用程序中的方法是否可以在没有windows服务的情况下使用服务用户帐户(域)模拟执行(使用Process.Start) 如果可能,如何实现这一点?作品: WPF应用程序中的方法是否可以在没有windows服务的情况下使用服务用户帐户(域)模拟执行(使用Process.Start) 无论调用进程是什么类型,都可以模拟用户。i、 WPF、Windows服务、控制台应用程序。没关系。但是,在Windows Vista及更高版本上,进程必须以管理员身份运行

我们希望在服务帐户的上下文中运行进程。 WPF应用程序中的方法是否可以在没有windows服务的情况下使用服务用户帐户(域)模拟执行(使用Process.Start)

如果可能,如何实现这一点?

作品:

WPF应用程序中的方法是否可以在没有windows服务的情况下使用服务用户帐户(域)模拟执行(使用Process.Start)

无论调用进程是什么类型,都可以模拟用户。i、 WPF、Windows服务、控制台应用程序。没关系。但是,在Windows Vista及更高版本上,进程必须以管理员身份运行

示例由

有关详细信息和完整示例的信息,我建议查看上面的链接,因为我不想引用整个示例

更多

尝试了不同的方法,但仍然收到消息:“登录失败:用户在此计算机上没有所需的登录类型”我们方法的复杂之处在于,我们有非管理员用户,希望使用服务用户帐户来处理网络共享操作。但我认为问题是,服务用户没有登录机器的权限…感谢您的快速响应!如何通过为服务用户指定用户名、密码来实现此目的?@SiL3NC3 your'rewelcome。哎哟,我忘了包括重要的起始位——用户名/密码的来源。请参见编辑。在您的WPF应用程序中,您可以让用户在登录窗口中指定该应用程序,也可以硬编码该应用程序(用于添加此应用程序)模拟是否始终需要管理员权限?或者有没有一条没有它的路?不是所有的域用户都有它们。。。我们将查看提供的链接。谢谢你。@SiL3NC3只用于Vista之前的Sadly那么我有麻烦了…;)谢谢你提供信息,伙计。
string userName, domainName;
// Get the user token for the specified user, domain, and password using the
// unmanaged LogonUser method.
// The local machine name can be used for the domain name to impersonate a user on this machine.
Console.Write("Enter the name of the domain on which to log on: ");
domainName = Console.ReadLine();

Console.Write("Enter the login of a user on {0} that you wish to impersonate: ", domainName);
userName = Console.ReadLine();

Console.Write("Enter the password for {0}: ", userName);

...

// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(userName, domainName, Console.ReadLine(),
                LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
                out safeTokenHandle);
...

using (safeTokenHandle)
{
...

    using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
    {
        using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
        {
            // Check the identity.
            Console.WriteLine("After impersonation: "
                             + WindowsIdentity.GetCurrent().Name);
        }
    }
}