windows 在VB.NET中以管理员身份自动运行程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8077685/
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 18:26:28  来源:igfitidea点击:

Automatically running a program as administrator in VB.NET

windowsvb.netwindows-7administrator

提问by Yoosuf

I am developing a VB.NET based application which requires administrative privileges in order to run properly. However I went through several tutorials online and it all mentioned how to do this using a mainifest file. The problem with this technique is that whenever the program is opened I need to type in the Administrator password, however I want this to happen automatically. (The user should not type the Admin password, the password must be entered from the code level)

我正在开发一个基于 VB.NET 的应用程序,它需要管理权限才能正常运行。然而,我在网上浏览了几个教程,它们都提到了如何使用 mainifest 文件来做到这一点。这种技术的问题在于,无论何时打开程序,我都需要输入管理员密码,但我希望这会自动发生。(用户不要输入Admin密码,密码必须从代码级别输入)

The following code is the closest I could find, but it too does not satisfy my problem,

以下代码是我能找到的最接近的代码,但它也不能满足我的问题,

Dim securePass As New Security.SecureString()
Dim pass As String = "password"

For Each c As Char In pass
    securePass.AppendChar(c)
Next

' If there isn't a domain, set the argument to 'Nothing', as demonstrated below.
Process.Start("testsoftware\WinKBSel\WinKBSel1003.exe", "username", pass, Nothing)

Could anyone please let me know how I could do this without requiring the user to enter the administrator password?

任何人都可以让我知道如何在不要求用户输入管理员密码的情况下执行此操作吗?

回答by JonAlb

You would probably be best splitting the application up, if you had a back-end service that runs under an administrator account your application could use that to do stuff it requires at an elevated privilege. UAC is designed to protect the user from themselves, you can get around it by adding a backend service... but be very careful about opening up holes in this way as they could be exploited by other applications/viruses. Storing an administror password inside an application is always a bad idea as the application could be decompiled and the password extracted.

如果您有在管理员帐户下运行的后端服务,您可能最好将应用程序拆分,您的应用程序可以使用它来以提升的权限执行所需的操作。UAC 旨在保护用户免受自身侵害,您可以通过添加后端服务来绕过它……但要非常小心地以这种方式打开漏洞,因为它们可能会被其他应用程序/病毒利用。将管理员密码存储在应用程序中总是一个坏主意,因为应用程序可能会被反编译并提取密码。

回答by Dan Puzey

You cannot create a program that will automatically log on as a user unless the username/password is already known.

除非用户名/密码已知,否则您无法创建将自动以用户身份登录的程序。

That means that you're either restricted to running on systems where your app already knows the credentials, your user has to enter the credentials into your app, or your user has to use the "Run As" functionality in Windows to use the correct user.

这意味着你要么被限制在你的应用程序已经知道凭据的系统上运行,你的用户必须在你的应用程序中输入凭据,或者你的用户必须使用 Windows 中的“运行身份”功能来使用正确的用户.

If what you're proposing were actually possible, it would represent a hugesecurity hole: you'd be able to run any code as any user without requiring authentication first. That is not a good thing!

如果您提出的建议确实可行,那么这将代表一个巨大的安全漏洞:您可以以任何用户身份运行任何代码,而无需先进行身份验证。那可不是什么好事!

Think about it like this: if you were sold a program that would automatically log on as administrator even without your permission, how would you feel?

想一想:如果你被卖了一个程序,即使没有你的许可,它也会自动以管理员身份登录,你会有什么感觉?

回答by David Heffernan

UAC is designed so that you cannot do what you want. UAC ensures that in order to elevate the user must pass through a UAC dialog (either the admin consent dialog or the over-the-shoulder dialog). You need to adapt your expectations to the reality of UAC.

UAC 的设计使您无法为所欲为。UAC 确保为了提升用户必须通过 UAC 对话(管理员同意对话或过肩对话)。您需要根据 UAC 的现实调整您的期望。

回答by majdman

You don't need a code for this action, UAC is made so you can automatically log on without the user's permission. What you can do is use the manifest and change the startup so when the application starts it runs as administrator and asks for permission! Change the manifest. look it up!

此操作不需要代码,UAC 已生成,因此您可以在未经用户许可的情况下自动登录。您可以做的是使用清单并更改启动,以便在应用程序启动时以管理员身份运行并请求许可!更改清单。查查!