对于非管理员用户,本地主机上的 HttpAddUrl 失败
使用 Windows HTTP API,我在本地主机上运行 HTTP 文件服务器。
这涉及到调用HttpAddUrl(hRequestQueue, L"http://localhost:80/", NULL)
。
除非用户以管理员身份运行应用程序,否则此操作将失败并显示 ERROR_ACCESS_DENIED
。我需要为没有管理员权限的用户提供此功能。 (无论如何,用户运行本地主机服务器有什么问题?这只是针对用户自己的。)
我找到了一个 修补程序Vista 和 XP 似乎旨在解决此问题,但 Windows 7 没有任何解决方案。该文章暗示它已在 Vista SP1 中修复,而我有 Windows 7 SP1,但它仍然是一个问题 - 修复是否没有成功到Windows 7?
我还能做些什么来让服务器为非管理员运行吗?
Using the Windows HTTP API I'm running a HTTP file server on localhost.
This involves calling HttpAddUrl(hRequestQueue, L"http://localhost:80/", NULL)
.
This fails with ERROR_ACCESS_DENIED
unless the user runs the application as administrator. I need this functionality for users who don't have admin privileges. (What's wrong with a user running a localhost server anyway? It's just for the user themselves.)
I found a hotfix for Vista and XP which seems aimed at solving this, but there's nothing for Windows 7. The article implies it was fixed in Vista SP1, and I have Windows 7 SP1 and it's still a problem - did the fix not make it to Windows 7?
Is there anything else I can do to get the server to run for non-admins?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答我自己的问题,但找到了解决方法:IANA 端口号 状态端口 49152-65535用于动态/私人目的。对于非管理员来说,端口 >= 49152 上的本地主机的 HttpAddUrl 可以正常工作。
Answering my own question, but found a workaround: the IANA port numbers state ports 49152-65535 are for dynamic/private purposes.
HttpAddUrl
for localhost on a port >= 49152 works fine for non-admins.这必须在系统级别进行配置,因为 HTTP API 使用 http.sys(内核驱动程序)。您可以使用以管理员权限执行的 netsh.exe 命令来授予用户或应用程序访问权限:
This must be configured on system level because HTTP API uses http.sys (kernel driver). You can use netsh.exe command executed with Administrator privileges to grant access to the user or application:
默认情况下,端口
1
-1024
需要管理访问权限。否则,您会收到错误代码 5 (ACCESS_DENIED
)。如果您尝试绑定到高于 1024 的端口,例如:它将适用于非管理员用户。在您的情况下,您尝试侦听端口
80
,HttpServer API 将该端口限制为管理员。Windows 中的一切都由访问控制列表 (ACL) 控制;这包括使用 HttpServer 时允许的侦听端口。您可以通过运行以下命令来显示 http 使用的当前 ACL:
如果您这样做,您将看到各种系统已定义的许多 ACL。
Windows Communication Foundation
一个 ACL 条目特别有趣:
每个人都被授予侦听端口
80
的权利,只要您靠的是:此 url 由 Windows Communication Foundation (WCF) 使用,通常构造一个以下形式的 URL:
这也意味着,如果您确实想要端口 80,您可以用自己的端口监听,例如:
只要没有人已经在使用端口 80(我正在查看你的 Skype!),你会 得到它。
WinSock 监听套接字不需要 admin
虽然 HttpServer API 有 ACL 控制对
1024
以下端口的访问,但应该注意的是,WinSock API 没有任何限制。如果您想使用 WinSock 在端口
80
上打开侦听套接字,您不需要是管理员。只有Http
api 具有 ACL。Ports
1
-1024
, by default, require administrative access. Otherwise you get error code 5 (ACCESS_DENIED
). If you attempt to bind to a port above 1024, e.g.:it will work for non-admin users. In your case you tried to listen on port
80
, which HttpServer API limits to administrators.Everything in Windows is controlled by Access Control Lists (ACLs); this includes the listen ports allowed when using HttpServer. You can display the current ACLs used by http by running:
If you do that, you'll see a lot of ACLs already defined by various systems.
Windows Communication Foundation
One ACL entry is particularly interesting:
Everyone is granted the right to listen on port
80
, as long as you live off of:This url is used by Windows Communication Foundation (WCF), which normally constructs a URL of the form:
It also means, if you really want port 80, you can listen with your own, for example:
As long as nobody is already using port 80 (i'm looking at your Skype!), you'll get it.
WinSock listening sockets do not require admin
While the HttpServer API has ACLs controlling access to ports below
1024
, it should be noted that the WinSock API has no restriction.If you want to use WinSock to open a listening socket on port
80
, you do not need to be an administrator. It is only theHttp
api that has the ACL.