2

我编写了一个程序,需要提升运行才能调用 WMI 对象的两种方法。如果由管理员组中的用户在“提升模式”下运行,它就像一个魅力。但现在的问题是它需要由每个人运行,而不是每个人都在他们工作的机器上拥有管理权限。所以我决定让程序重新启动自己,模拟“域管理员”组中的用户。

我的代码如下:

 ProcessStartInfo proc = new ProcessStartInfo();
 proc.WorkingDirectory = Environment.CurrentDirectory;
 proc.FileName = System.Windows.Forms.Application.ExecutablePath; ;
 proc.UserName = "USERNAME";
 SecureString secure = new SecureString();
 foreach (var item in "PASSWORD")
 {
     secure.AppendChar(item);
 }
 proc.Password = secure;
 proc.Domain = "DOMAIN";
 proc.UseShellExecute = false;
 proc.Verb = "runas";
 Process.Start(proc);

启用 UAC 时有两个问题。首先,如果我没有明确地将应该被模拟的用户添加到程序的 installfolder 的 ACL 中(用户已经在管理员组中,该组对文件夹和其中包含的所有文件具有完全访问权限)它会抛出一个System.ComponentModel.Win32Exception: The directory name is invalid. 我仔细检查了目录路径,它实际上是有效的(当用户明确地在 ACL 中时有效)。

现在,如果明确添加了用户并且没有例外,则程序以用户身份运行但没有提升的权限,因此调用不起作用。

我究竟做错了什么?谢谢你的帮助。

我知道已经有一些关于类似问题的问题,但没有一个解决方案/答案解决了我的问题,或者它们不够详细并且太长时间处于非活动状态。

4

1 回答 1

2

我遇到了同样的问题,我无法同时提升和提供凭据。为了使用“runas”动词,您必须设置 UseShellExecute=true,但为了提供凭据,您必须将其设置为 false。我最终做的是测试管理员权限,如果它们不存在,则以具有本地管理员权限的单独用户身份重新启动我的应用程序。然后,我将使用“runas”动词运行 process.start,而无需提供凭据。此时,您的用户将收到 UAC 提示,选择“是”后,该进程将运行提升。我在 VB 中编写了我的,但想法是一样的,这是我的代码,IsUserAdminGroup 的使用来自:

UAC自提升


     Try
    'check if user is administrator
    Dim fInAdminGroup As Boolean = Me.IsUserInAdminGroup
    If (Me.IsUserInAdminGroup = False) Then
    'check if process is running under user you want to elevate
        If Not (System.Environment.UserName = "") Then
    'this process restarts the app as the desired admin user
            Dim path As String = Environment.CurrentDirectory
            Dim filename As String = Application.ExecutablePath
    'create secure string password
            Dim passwordPre As String = ""
            Dim passwordChars As Char() = passwordPre.ToCharArray()
            Dim password As New SecureString()
            For Each c As Char In passwordChars
                password.AppendChar(c)
            Next
            Dim procInfo As New System.Diagnostics.Process
            procInfo.StartInfo.FileName = filename
            procInfo.StartInfo.UserName = ""
            procInfo.StartInfo.Domain = ""
            procInfo.StartInfo.Password = password
            procInfo.StartInfo.UseShellExecute = False
    'load user profile was needed for a legacy application I used that failed without the user hive loaded
            'procInfo.StartInfo.LoadUserProfile = True
            procInfo.StartInfo.CreateNoWindow = True
            procInfo.StartInfo.WorkingDirectory = path
            procInfo.Start()
            Application.Exit()
        Else
            MsgBox("Unable to open, call support.", MsgBoxStyle.Critical, "Run Failed")
            Application.Exit()
        End If
    ElseIf (System.Environment.UserName = "") Then
        Me.Visible = False
        Me.Hide()
    'this process starts the desired app elevated
        Dim procInfo As New System.Diagnostics.Process
        procInfo.StartInfo.FileName = "ptpublisher.exe"
        procInfo.StartInfo.Verb = "runas"
        procInfo.StartInfo.UseShellExecute = True
        procInfo.StartInfo.CreateNoWindow = True
        procInfo.StartInfo.WorkingDirectory = "C:\Program Files (x86)\Primera Technology\PTPublisher"
        procInfo.StartInfo.Arguments = ""
        procInfo.Start()
        Application.Exit()
    End If
Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.Critical, "Run Failed")
    Application.Exit()
End Try
于 2013-09-13T15:00:06.393 回答