C# 非管理员的 HttpListenerException“访问被拒绝”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14962334/
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
HttpListenerException "access denied" for non-admins
提问by Mossi
I have written a C# application that uses HttpListener
to listen for HTTP requests -obviously! The namespace prefix I use is also registered using netsh
for the current user (as suggested by everyone on SO).
我编写了一个 C# 应用程序,用于HttpListener
侦听 HTTP 请求 - 显然!我使用的命名空间前缀也是netsh
为当前用户注册的(正如 SO 上的每个人所建议的那样)。
The problem is despite using netsh my application still throws an "access is denied" exception for non-admin users. The OS is Windows 7.
问题是尽管使用 netsh 我的应用程序仍然为非管理员用户抛出“访问被拒绝”异常。操作系统为 Windows 7。
Update: It appears as though my application is not executing the netsh
command when I run it with a non-admin user. Is there any problems with my code? There are no exceptions thrown.
更新:netsh
当我使用非管理员用户运行它时,我的应用程序似乎没有执行该命令。我的代码有问题吗?没有抛出异常。
AddAddress("http://localhost:8400/", Environment.UserDomainName, Environment.UserName);
HttpListener _listener = new HttpListener();
_listener.Prefixes.Add("http://localhost:8400/");
_listener.Start();
...
/** method stolen from an SO thread. sorry can't remember the author **/
static void AddAddress(string address, string domain, string user)
{
string args = string.Format(@"http add urlacl url={0}", address) + " user=\"" + 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 Jim Mischel
The line that I use when I'm doing an HttpListener is:
我在做 HttpListener 时使用的行是:
netsh http add urlacl url=http://+:8008/ user=Everyone listen=yes
The user can be an individual user or a user group. So if you want only Administrators to have access, for example:
用户可以是个人用户或用户组。因此,如果您只希望管理员具有访问权限,例如:
netsh http add urlacl url=http://+:8008/ user=Administrators listen=yes
You can get help on the command:
您可以获得有关命令的帮助:
netsh http add urlacl help
Note that the url=
is optional. Also, older versions of the command require true or false for the listen parameter. Current versions use yes or no.
请注意,url=
是可选的。此外,旧版本的命令需要 true 或 false 的 listen 参数。当前版本使用是或否。