English 中文(简体)
如何显示 UAC 提示文件, 使用 c# 保存到限制位置?
原标题:How to display UAC prompt for file save to restricted location using c#?

当一个用户从我的应用程序中保存一个文件时, 他们目前无法保存到限制地点( 如 C: ) 。 我认为这是一个很好的限制, 但我想提供 UAC 提示, 提升权限并允许用户在限制区保存 。

围绕这个话题我看到了许多答案,这些答案涉及利用 runas 生成一个拥有更高权限的新进程。 而且,这似乎可以通过假冒另一个用户来完成。 据我所知,这两种方法都需要用户提供用户证书。

我想做的基本上就是Windows自己做的。当您尝试将文件复制到 C: 在 Windows 7 (假设您已将 UAC 设定为默认水平) 中, 您会得到以下提示 :

"https://i.sstatic.net/hFLAX.png" alt="UAC 提示"/>

一旦您单击使用 UAC 屏蔽的“ 继续” 按钮, 文件将被复制到 C: 没有证书提示( 假设您用管理员权限重新登录) 。

我怎样才能在我的行政用户应用程序中复制这种行为? 他们不应该冒充任何其他用户,因为他们已经拥有管理特权。 任何人都能提供Windows在此过程中所做的细节吗? 他们是否正在产生一个新的具有更高特权的探索者. exe 进程?

最佳回答

您需要做Windows 的工作。 并生成一个带有高权限运行的新进程。 这里没有快捷键。 当进程启动时分配的符号是决定进程拥有的权利。 该符号在进程启动后无法更改。 如果您需要提升, 您需要一个新进程 。

围绕这个话题我看到了许多答案,这些答案涉及利用 runas 生成一个拥有更高权限的新进程。 而且,这似乎可以通过假冒另一个用户来完成。 据我所知,这两种方法都需要用户提供用户证书。

否。如果当前用户不是管理员,那么 UAC 对话框将提示拥有管理员权限的用户的新证书。那就是 < em> over- the shouler UAC 对话框。另一方面,如果当前用户是管理员,那么他们只能获得 < em > consent 对话框。这就是在安全桌面上显示的对话框,只是要求您点击“ 继续 ” 。

Windows 组件可以做到的一件事是, Windows 组件可以在不显示同意对话框的情况下启动一个进程。 仅在 Windows 7 上( 不在 Vista 上) 发生, 并且只有在 Windows 7 添加的新 < em> Default < / em > 设置有 UAC 设置的情况下发生。 仅允许 Windows 7 添加到该设置, 说明Explorer 如何能够显示您包含在问题中的对话框, 然后在不显示同意 UAC 对话框的情况下启动一个高级程序来复制该程序。 只有 Windows 组件才被赋予此能力 。

但底线是,您需要启动一个跳升的新进程。 使用 < code> runas 动词是这样做的明理方式 。

问题回答

提高特权方案/UAC

Running applications with more privileges than required is against the principle of least privilege, and may have potential security vulnerability. To defend this, Windows Vista introduces User Account Control (UAC), to protect operating system by running applications with reduced privileges (as a normal user), even the current user is signed in as an administrator. More and more XP/2K users are also use normal user account for daily use. Read UAC Demystified first to fully understand UAC.

开发者往往犯两个常见错误: < / strong>

  • Request the end-user to run an application with administrator privilege even though this is not necessary, most of the time because of bad design practices. These applications either scare away end-users, or potentially have security vulnerability.
  • Do not request the end-user to run the application elevated but try to perform operations that require administrator privilege. These applications simply break under Windows Vista or Windows XP/2K normal user account.

The downloadable sample code demonstrates how to programming elevated privilege/UAC. Both WPF and Windows Forms sample applications are provided. Run the application for the following scenarios to see the difference:

  • Normal user, Windows XP/Windows Vista: the UAC shield icon is displayed. Clicking “Save to C:” displays “Run As” dialog, asking user to enter administrator password to continue;
  • Administrator, Windows XP/Windows Vista with UAC disabled: the UAC shield icon is hidden. Clicking “Save to C:” completed without any dialog;
  • Administrator, Windows Vista with UAC enabled: the UAC shield icon is displayed. Clicking “Save to C:” displays dialog asking user’s permission to continue.

下载链接

调用高端执行( 首先测试管理员 ) :

private void SaveToRootFolder_Click(object sender, EventArgs e)
    {
        string fileName = @"C:Test.txt";
        if (App.IsAdmin)
            DoSaveFile(textBox1.Text, textBox2.Text, fileName);
        else
        {
            NameValueCollection parameters = new NameValueCollection();
            parameters.Add("Text1", textBox1.Text);
            parameters.Add("Text2", textBox2.Text);
            parameters.Add("FileName", fileName);
            string result = Program.ElevatedExecute(parameters);
            if (!string.IsNullOrEmpty(result))
                MessageBox.Show(result);
        }
    }

高级执行 :

internal static string ElevatedExecute(NameValueCollection parameters)
    {
        string tempFile = Path.GetTempFileName();
        File.WriteAllText(tempFile, ConstructQueryString(parameters));

        try
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.WorkingDirectory = Environment.CurrentDirectory;
            Uri uri = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase);
            startInfo.FileName = uri.LocalPath;
            startInfo.Arguments = """ + tempFile + """;
            startInfo.Verb = "runas";
            Process p = Process.Start(startInfo);
            p.WaitForExit();
            return File.ReadAllText(tempFile);
        }
        catch (Win32Exception exception)
        {
            return exception.Message;
        }
        finally
        {
            File.Delete(tempFile);
        }
    }

有限选项, 仅在移动、 重命名、 复制和删除文件时使用 :

SHFile Operation

如果您试图通过此函数执行文件操作, Windows 将向用户提供高亮提示。

请注意,这方面有一些缺点:

  • This only works for Moving, Renaming, Copying, and Deleting. Saving a new file this way would require saving to a temp directory, then Moving it to the desired location. This does not solve the problem of the Save File Dialog not allowing you to select a UAC protected location as a target.
  • If the target directory doesn t exist (for a Move or Copy), SHFileOperation can prompt the user if the target directory should be created. However, it will NOT ask for elevated privileges to do so, and so will fail under a UAC protected location. The workaround for this is to manually create the non-existent directories in a temporary location, then Move/Copy them to the target location. This WILL provide the UAC prompt.
  • You need to have contingency plans in place for if the user selects Skip or Cancel to the Move/Copy dialog, or if the user selects No at the UAC prompt.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签