使用特定用户帐户运行 Windows 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6062030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:52:40  来源:igfitidea点击:

Run a Windows App using a specific User account

c#windowsexeapplication-settings

提问by Robert

I need to ensure that my widnows app (winform not console) runs under a certain user account (in other words any user can be logged on to the maching but the .exe will always execute as the specified user).

我需要确保我的 widnows 应用程序(winform 不是控制台)在某个用户帐户下运行(换句话说,任何用户都可以登录到机器,但 .exe 将始终以指定用户的身份执行)。

Can this be done programtically? If so, how?

这可以以编程方式完成吗?如果是这样,如何?

回答by Simon Mourier

You can start the application like this:

您可以像这样启动应用程序:

ProcessStartInfo psi = new ProcessStartInfo(myPath);
psi.UserName = username;

SecureString ss = new SecureString();
foreach (char c in password)
{
 ss.AppendChar(c);
}

psi.Password = ss;
psi.UseShellExecute = false;
Process.Start(psi);

回答by Aaron Klotz

One thing you could do in your app is check if you're running as the desired user, and if not, create a new instance of your app as that other user. The first instance would then exit.

您可以在您的应用程序中做的一件事是检查您是否以所需用户的身份运行,如果不是,则以该其他用户的身份创建您的应用程序的新实例。然后第一个实例将退出。

To check which user you are running as, you could adapt the solution from hereso that the process queries itself for its token information.

要检查您以哪个用户身份运行,您可以从这里调整解决方案,以便流程自行查询其令牌信息。

Use CreateProcessWithLogonW, passing the LOGON_WITH_PROFILElogin flag. The user you are logging in as must have the appropriate policies set to be allowed to log on interactively.

使用CreateProcessWithLogonW,传递LOGON_WITH_PROFILE登录标志。您登录的用户必须设置适当的策略才能允许交互登录。

EDIT: Now that you have indicated that you are using .NET, here's how you should do it:

编辑:既然您已经表明您正在使用 .NET,那么您应该这样做:

First you need to find out which user you are currently running as. Use the WindowsIdentityclass from the System.Security.Principalnamespace. Call its GetCurrentmethod to obtain the WindowsIdentityobject for the user that you are running as. The Nameproperty will give you the actual user name that you are running under.

首先,您需要找出您当前以哪个用户身份运行。使用命名空间中的WindowsIdentitySystem.Security.Principal。调用它的GetCurrent方法来WindowsIdentity为您正在运行的用户获取对象。该Name属性将为您提供您正在运行的实际用户名。

In your ProcessStartInfoobject, set LoadUserProfile = true, the FileNamefield, possibly the Argumentsfield, the UserNameand Passwordfields, possibly the Domainfield, and set UseShellExecute = false. Then call Process.Start(), passing in your ProcessStartInfoobject.

在您的ProcessStartInfo对象中, set LoadUserProfile = trueFileName字段(可能是Arguments字段)、UserNamePassword字段(可能是Domain字段)和 set UseShellExecute = false。然后调用Process.Start(),传入您的ProcessStartInfo对象。

Here's a sample that I threw together, but I don't have a C# compiler installed to test it:

这是我拼凑的一个示例,但我没有安装 C# 编译器来测试它:

using System;
using System.Diagnostics;
using System.Security;
using System.Security.Principal;

// Suppose I need to run as user "foo" with password "bar"

class TestApp
{
    static void Main( string[] args )
    {
        string userName = WindowsIdentity.GetCurrent().Name;
        if( !userName.Equals( "foo" ) ) {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "testapp.exe";
            startInfo.UserName = "foo";

            SecureString password = new SecureString();
            password.AppendChar( 'b' );
            password.AppendChar( 'a' );
            password.AppendChar( 'r' );
            startInfo.Password = password;

            startInfo.LoadUserProfile = true;
            startInfo.UseShellExecute = false;

            Process.Start( startInfo );    
            return;
        }
        // If we make it here then we're running as "foo"
    }
}