“Win32Exception:目录名称无效”模拟& UAC

时间:2013-03-12 08:19:56

标签: c# windows-7 process uac impersonation

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

我的代码如下:

 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时有两个问题。 首先,如果我没有明确地将应该模拟的用户添加到程序的安装文件夹的ACL中(用户已经在Administrators组中,该组具有对文件夹及其中包含的所有文件的完全访问权限),则会抛出{{1} }。 我仔细检查了目录路径,它实际上是有效的(并且当用户明确地在ACL中时有效)。

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

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

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

1 个答案:

答案 0 :(得分:2)

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

UAC self-elevation


     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