在VB.NET中以管理员身份自动运行程序
我正在开发一个基于 VB.NET 的应用程序,它需要管理权限才能正常运行。不过,我在网上浏览了几个教程,它们都提到了如何使用 mainifest 文件来执行此操作。这种技术的问题是,每当打开程序时,我都需要输入管理员密码,但我希望这种情况自动发生。 (用户不应输入管理员密码,必须从代码级别输入密码)
以下代码是我能找到的最接近的代码,但它也不能满足我的问题,
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)
任何人都可以让我知道如何做到这一点不需要用户输入管理员密码?
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)
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您有一个在管理员帐户下运行的后端服务,您的应用程序可以使用它来以提升的权限执行所需的操作,那么您可能最好将应用程序拆分。 UAC 旨在保护用户免受自身侵害,您可以通过添加后端服务来绕过它……但要非常小心地以这种方式打开漏洞,因为它们可能会被其他应用程序/病毒利用。在应用程序中存储管理员密码始终是一个坏主意,因为应用程序可能会被反编译并提取密码。
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.
除非用户名/密码已知,否则您无法创建自动以用户身份登录的程序。
这意味着您要么被限制在您的应用程序已经知道凭据的系统上运行,您的用户必须将凭据输入到您的应用程序中,要么您的用户必须使用 Windows 中的“运行方式”功能才能使用正确的用户。
如果您提出的建议实际上可行,那么它将代表一个巨大的安全漏洞:您可以以任何用户身份运行任何代码,而无需首先进行身份验证。那不是什么好事!
试想一下:如果你被卖给了一个即使没有你的许可也会自动以管理员身份登录的程序,你会有什么感觉?
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.
If what you're proposing were actually possible, it would represent a huge security 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?
UAC 的设计使您无法做您想做的事。 UAC 确保为了提升权限,用户必须通过 UAC 对话框(管理员同意对话框或越肩对话框)。您需要根据 UAC 的实际情况调整您的期望。
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 是为了让您无需用户许可即可自动登录。您可以做的是使用清单并更改启动方式,以便应用程序启动时它以管理员身份运行并请求许可!更改清单。查一下!
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!