我可以在不需要管理员权限的情况下侦听 Vista 上的端口(使用 HttpListener 或其他 .NET 代码)吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/169904/
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
Can I listen on a port (using HttpListener or other .NET code) on Vista without requiring administrator priveleges?
提问by Jon Galloway
I'm using HttpListener to allow a user to set up a proxy on a user-defined port. When I start the HttpListener, I get an exception if the application isn't running under administrator privileges in Vista.
我正在使用 HttpListener 来允许用户在用户定义的端口上设置代理。当我启动 HttpListener 时,如果应用程序不是在 Vista 中的管理员权限下运行,我会收到一个异常。
From what I've read, this is expected behavior- administrator privileges are required to start listening on a port. But I'm sure there are ways around this, as I run plenty of programs (like Skype) which listen on a port without requiring elevation to administrator.
根据我的阅读,这是预期的行为- 需要管理员权限才能开始侦听端口。但我确信有办法解决这个问题,因为我运行了很多程序(如 Skype),它们在不需要提升到管理员权限的情况下监听端口。
Is there a way to do this with HttpListener? If not, can I make other API calls in .NET code to set up the port?
有没有办法用 HttpListener 做到这一点?如果没有,我可以在 .NET 代码中进行其他 API 调用来设置端口吗?
采纳答案by Greg Hewgill
I've never used an HttpListener, but from your description it sounds more like you want to listen on a regular TCP port, instead of embedding your application into a server URL namespace (which is what HttpListener appears to do). You should be able to use regular socket functions (System.Net.Sockets.TcpListener) to open and listen on a TCP port without requiring administrator privileges. I'm almost certain Skype doesn't use an HttpListener.
我从未使用过 HttpListener,但从您的描述来看,这听起来更像是您想在常规 TCP 端口上侦听,而不是将您的应用程序嵌入到服务器 URL 命名空间中(这正是 HttpListener 所做的)。您应该能够使用常规套接字函数 (System.Net.Sockets.TcpListener) 打开和侦听 TCP 端口,而无需管理员权限。我几乎可以肯定 Skype 不使用 HttpListener。
回答by Roger Lipscombe
While you can write your own HTTP server using normal TCP/IP (it's relatively simple), it is easier to use HttpListener, which takes advantage of the HTTP.SYS functionality added in Windows XP SP2.
虽然您可以使用普通的 TCP/IP 编写自己的 HTTP 服务器(它相对简单),但使用 HttpListener 更容易,它利用了 Windows XP SP2 中添加的 HTTP.SYS 功能。
However, HTTP.SYS adds the concept of URL ACLs. This is partly because HTTP.SYS allows you to bind to sub-namespaces on port 80. Using TCP/IP directly avoids this requirement, but means that you can't bind to a port that's already in use.
但是,HTTP.SYS 添加了 URL ACL 的概念。这部分是因为 HTTP.SYS 允许您绑定到端口 80 上的子命名空间。直接使用 TCP/IP 可避免此要求,但意味着您无法绑定到已在使用的端口。
On Windows XP, you can use the HttpCfg.exe program to set up a URL ACL granting your user account the right to bind to a particular URL. It's in the Platform SDK samples.
在 Windows XP 上,您可以使用 HttpCfg.exe 程序设置 URL ACL,授予您的用户帐户绑定到特定 URL 的权限。它位于 Platform SDK 示例中。
On Windows Vista, HTTPCFG is still supported, but the functionality has been absorbed into NETSH:
在 Windows Vista 上,仍然支持 HTTPCFG,但该功能已被吸收到 NETSH 中:
netsh http show urlacl
...will show a list of existing URL ACLs. The ACLs are expressed in SDDL.
...将显示现有 URL ACL 的列表。ACL 以 SDDL 表示。
netsh http add urlacl url=http://+:80/MyUri user=DOMAIN\User listen=yes
...will configure the MyURI namespace so that DOMAIN\User can listen to requests.
...将配置 MyURI 命名空间,以便 DOMAIN\User 可以侦听请求。
回答by Lu55
If you need to handle requests only from you own computer (usually for test purposes), you can write localhostinstead of * in prefix.
如果您只需要处理来自您自己的计算机的请求(通常用于测试目的),您可以在前缀中写入localhost而不是 *。
For example, instead of "http://*:9669/" you can write "http://localhost:9669/". This works fine with HttpListener and doesn't require administrative privileges (at least on Windows 7).
例如,您可以编写“http://localhost:9669/”而不是“http://*:9669/”。这适用于 HttpListener 并且不需要管理权限(至少在 Windows 7 上)。
回答by rob
Well I had to deal with something similar. My Computer is in a restricted domain, so I don't have administrator privileges. After some research and reading I found this thread and the netsh hints made me use temporary acl bindings just for developing tests. On my computer these rule exists.
好吧,我不得不处理类似的事情。我的电脑在受限域中,所以我没有管理员权限。经过一些研究和阅读,我发现了这个线程,netsh 提示让我使用临时 acl 绑定来开发测试。在我的电脑上存在这些规则。
There's this entry:
有这个条目:
Run 'netsh http show urlacl' (as shown above)
运行'netsh http show urlacl'(如上图)
[...]
Reservierte URL : http://+:80/Temporary_Listen_Addresses/
Benutzer: \Jeder
Abh?ren: Yes
Delegieren: No
SDDL: D:(A;;GX;;;WD)
[...]
So I can use the HttpListener as non-admin (Jeder):
所以我可以使用 HttpListener 作为非管理员(Jeder):
[...]
HttpListener l = new HttpListener();
string prefix = "http://+:80/Temporary_Listen_Addresses/";
l.Prefixes.Add(prefix);
l.Start(); // does not throw any "Permission Denied/Access Denied/Zugriff verweigert"
[...]
May this helps anybody finding this thread.
可能这有助于任何人找到这个线程。

