windows C# HttpListener 不使用 netsh 注册 URI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2583347/
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
C# HttpListener without using netsh to register a URI
提问by Christopher Tarquini
My application uses a small webserver to server up some files and have a web interface for administration remotely. Right now the user has to use netsh to register the URI like so
我的应用程序使用一个小型网络服务器来处理一些文件,并有一个用于远程管理的网络界面。现在用户必须像这样使用 netsh 来注册 URI
netsh http add urlacl url=http://+:1233/ user=Chris-PC\Chris
Which is no fun for the average user. I'd like the program to be able to listen on any port specified by the user from my program without the end-user needing to using command prompt. Is there anyway to accomplish this short of just using Process.Start and running command prompt myself?
这对普通用户来说并不有趣。我希望该程序能够从我的程序中侦听用户指定的任何端口,而最终用户无需使用命令提示符。无论如何,除了自己使用 Process.Start 并运行命令提示符之外,还有什么方法可以解决这个问题吗?
回答by chillitom
I wrote this to elevate perms and add http ACL entries through netsh.
我写这个是为了提升权限并通过netsh添加 http ACL 条目。
Users will get prompted to make changes top their system but it's better than nothing. You might want to do this in response to an AddressAccessDeniedException
用户会收到提示在他们的系统上进行更改,但总比没有好。您可能希望这样做以响应AddressAccessDeniedException
public static class NetAclChecker
{
public static void AddAddress(string address)
{
AddAddress(address, Environment.UserDomainName, Environment.UserName);
}
public static void AddAddress(string address, string domain, string user)
{
string args = string.Format(@"http add urlacl url={0} user={1}\{2}", address, domain, user);
ProcessStartInfo psi = new ProcessStartInfo("netsh", args);
psi.Verb = "runas";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = true;
Process.Start(psi).WaitForExit();
}
}
回答by Damien
If you are using an HTTPListener, you have to use httpconfig.exe or netsh to configure your HTTPListener, because:
如果您使用的是 HTTPListener,则必须使用 httpconfig.exe 或 netsh 来配置您的 HTTPListener,因为:
A. AFAIK There is no API for doing this from C#, only the command line tools
A. AFAIK 没有从 C# 执行此操作的 API,只有命令行工具
B. By default only SYSTEM or the local Administrators group can listen to http prefixes
B、默认只有SYSTEM或本地Administrators组可以监听http前缀
So... if your app is running under the user's account, you need to explicitly grant access (using httpcfg.exe or netsh) because your HTTPHandler uses the HTTP.sys to share ports with the running webserver (so you can host your own web app on port 80 while IIS is running on port 80 as well).
所以...如果您的应用程序在用户帐户下运行,您需要明确授予访问权限(使用 httpcfg.exe 或 netsh),因为您的 HTTPHandler 使用 HTTP.sys 与正在运行的网络服务器共享端口(因此您可以托管自己的Web 应用程序在端口 80 上运行,而 IIS 也在端口 80 上运行)。
A better solution might be to use an actual embedded web server to listen on the port you need. This won't work if another application (e.g. IIS) is already using the port, but since this DOESN'T use the HTTP.sys for port-sharing, there are no security restrictions about what user account is opening /listening on the port. Because you want the user to specify their own port, it seems acceptable to run on another port besides port 80.
更好的解决方案可能是使用实际的嵌入式 Web 服务器来侦听您需要的端口。如果另一个应用程序(例如 IIS)已经在使用该端口,这将不起作用,但由于这不使用 HTTP.sys 进行端口共享,因此对于正在打开/侦听端口的用户帐户没有安全限制. 因为您希望用户指定自己的端口,所以在端口 80 之外的另一个端口上运行似乎是可以接受的。
My company makes a commercial product called the Neokernel Web Server (http://www.neokernel.com) that runs ASP.NET and lets you programmatically start/stop the server and set configurations; you can embed and distribute this assembly with the application.
我公司生产了一个商业产品,叫做 Neokernel Web Server ( http://www.neokernel.com),它运行 ASP.NET 并让你以编程方式启动/停止服务器并设置配置;您可以在应用程序中嵌入和分发此程序集。
There is also the Cassini source code which you can modify and embed, it's free but has a couple of drawbacks like no logging or SSL support.
还有你可以修改和嵌入的 Cassini 源代码,它是免费的,但有一些缺点,比如没有日志记录或 SSL 支持。