使用管理员权限运行部分 ac# 程序
有没有办法向操作系统请求提升权限,仅用于 ac# 程序的“一部分”?
我正在使用 NUnit 编写一堆集成测试。我正在测试的一件事是被测应用程序是否正确连接到端口 843。测试在端口 843 打开一个监听套接字,然后向被测应用程序抛出各种响应,并验证应用程序是否行为正确。
然而,在端口 843 上打开侦听套接字需要管理员权限。
我想找到侵入性最小的方式来运行此测试。我可以以 root/admin 身份运行整个 NUnit 套件,但这会使很多东西以 root 身份运行,实际上不需要以 root 身份运行,我想阻止这种情况。
Is there a way to request elevated privileges from the os, for "just a part" of a c# program?
I'm writing a bunch of integrationtests using NUnit. One of the things I'm testing is if the application under test correctly connects to port 843. The test opens a listening socket at port 843, and then throws all sorts of responses to the application under test, and verifies if the application behaves correctly.
Opening a listening socket on port 843 requires admin privileges however.
I'd like to find the least intrusive way to be able to run this test. I could run the entire NUnit suite as root/admin, but that would make a lot of stuff run as root, that really doesn't need to be ran as root, which I'd like to prevent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您可以查看 LogonUser 功能。这是一个示例。
Yes, you could take a look at the LogonUser function. Here's a sample.
必须以提升的特权启动进程才能获得提升的权利。您无法更改“正在处理”的提升状态。
解决这个问题的一种方法是使用任务管理器。如果您运行“未提升”并单击“为所有用户显示进程”,它基本上会杀死旧的任务管理器进程并启动一个具有提升权限的新进程来完成这项工作。
A process has to be started with elevated privileges to have elevated rights. You cannot change your elevated status "in process".
A way to work around this is to do as Task Manager. If you run that "unelevated" and click on "Show processes for all users", it basically kills of the old task manager process and starts a new one with elevated privileges in order to do the job.
如果需要,下面的代码将帮助您确定当前登录的用户是否是管理员:
If required below code would help you to find out if the current logged in user is admin or not:
没有。海拔要么全有,要么全无。通常,如果需要提升,应用程序会将自身引导到提升状态。
Nope. Elevation is all or nothing. Typically if elevation is required, the app bootstraps itself into an elevated state.