如何使用C#来显示文件的UAC提示保存到受限制的位置? [英] How to display UAC prompt for file save to restricted location using c#?

查看:216
本文介绍了如何使用C#来显示文件的UAC提示保存到受限制的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户从我的应用程序保存文件,他们目前无法保存受限位置(如C :)。我觉得这是一个很好的限制,但我想提供一个UAC提示提升权限,并允许用户在禁区保存。

When a user saves a file from my application, they currently can't save to restricted locations (like C:). I think this is a good restriction, but I would like to provide a UAC prompt to elevate privileges and allow a user to save in a restricted area.

我见过很多围绕这个话题的答案是涉及产卵一个新的进程,使用运行方式提升权限的。此外,它看起来这可以通过假冒其他用户来完成。据我了解,双方的这些方法需要用户提供用户凭据。

I've seen lots of answers around this topic that involve spawning a new process with elevated privileges using 'runas'. Also, it seems like this can be done by impersonating another user. From what I understand, both of those methods require a user to provide user credentials.

什么我想要做的基本上是Windows本身一样。当您尝试将文件复制到C:\ Windows 7中(假设你有UAC设置为默认级别),你会得到以下提示:

What I'm wanting to do is basically what Windows itself does. When you try to copy a file to C:\ in Windows 7 (assuming you've got UAC set to its default level), you get the following prompt:

一旦你点击Continue按钮与UAC的盾牌,将文件复制到C:\没有提示凭据(假设你登录使用管理员权限)。

Once you click the Continue button with the UAC shield, the file is copied to C:\ with no prompt for credentials (assuming you're logged on with admin privileges).

我怎么能复制我的admin用户的应用程序这种行为?他们不应该模仿任何其他用户,因为他们已经拥有管理员权限。任何人都可以提供在此过程中的Windows什么是做细节?他们是产卵以高特权新explorer.exe进程?

How can I replicate this behavior in my application for admin users? They shouldn't have to impersonate any other user because they already have admin privileges. Can anyone provide details on what Windows is doing during this process? Are they spawning a new explorer.exe process with elevated privileges?

推荐答案

您需要做的事情Windows不会。并产生一个新的过程,这将使用提升权限运行。这里没有捷径。一个进程启动时分配的令牌是什么决定了权利的进程。该过程开始后该标记不能改变。如果您需要提升,需要一个新的进程。

You need to do what Windows does. And spawn a new process which will run with elevated rights. There are no shortcuts here. The token that is allocated when a process starts is what determines what rights the process has. That token cannot be changed after the process has started. If you need to elevate, you need a new process.

我见过很多围绕这个话题的答案是涉及产卵一个新的进程使用运行方式提升权限。此外,它看起来这可以通过假冒其他用户来完成。据我了解,双方的这些方法需要用户提供用户凭据。

I've seen lots of answers around this topic that involve spawning a new process with elevated privileges using 'runas'. Also, it seems like this can be done by impersonating another user. From what I understand, both of those methods require a user to provide user credentials.

没有事实并非如此。如果当前用户不是管理员,那么UAC对话框会提示确实有管理员权限的用户的新凭证。那是的过肩的UAC对话框。在另一方面,如果当前用户是管理员然后他们刚刚拿到的同意的对话框。这是一个的显示在安全桌面上,只要求你单击继续对话框。

No that's not the case. If the current user is not an admin, then the UAC dialog will prompt for new credentials of a user that does have admin rights. That's the over-the-shoulder UAC dialog. On the other hand, if the current user is an admin then they just get the consent dialog. That's the dialog that's shown on the secure desktop and just asks for you to click Continue.

一件事,Windows组件可以做到这一点,你不能就是启动时不升高的过程显示你的同意对话框。这种情况发生在Windows 7只(未Vista系统),只有当你有新的默认的设置,在Windows 7中添加这就是浏览器是如何能够显示对话框中的UAC设置你列入问题,然后开始升高的过程中做复制,而不显示同意UAC对话框。只有Windows组件授予的能力。

The one thing that Windows components can do that you cannot is start a process elevated without showing you the consent dialog. That happens on Windows 7 only (not on Vista), and only if you have the UAC setting at the new Default setting that was added in Windows 7. That's how Explorer is able to show the dialog that you included in the question and then start an elevated process to do the copying without showing the consent UAC dialog. Only Windows components are granted that ability.

但底线是,你需要开始运行提升一个新的进程。使用运行方式动词是做到这一点的正规途径。

But the bottom line is that you need to start a new process that runs elevated. Using the runas verb is the canonical way to do it.

这篇关于如何使用C#来显示文件的UAC提示保存到受限制的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆