Running self-hosted OWIN Web API under non-admin a

2020-01-29 06:28发布

Is it possible for a self-hosted OWIN Web API to run under a non-administrator account? I have already tried dozens of url reservations and nothing works. The service fails to start with "Access is denied". It works when the account is added to the administrator role but I don't want that. Code below is running on Win 7 framework 4.5.2.

//install-package microsoft.owin.hosting
//install-package Microsoft.Owin.Host.HttpListener

StartOptions options = new StartOptions();
options.Urls.Add("http://localhost:5000/");
//options.Urls.Add(string.Format("http://{0}:5000", Environment.MachineName));
//options.Urls.Add("http://+:5000/");
//options.Urls.Add("http://*:5000/");

using (WebApp.Start<WebAPISelfHostMinimal.Startup>(options))
{
    while (!Terminate)
    {
        await Task.Delay(10); //keep cpu from getting pegged
    }

    LogUtil.LogInfo("Terminating owin host.");
}

EDIT - this is running under a Windows account.

C:\>netsh http add urlacl http://+:5000/ user=mini2012\svcAPI

URL reservation successfully added

C:\>sc start apiservice
[SC] StartService FAILED 5:

Access is denied.

C:\>netsh http add urlacl http://*:5000/ user=mini2012\svcAPI

URL reservation successfully added

C:\>sc start apiservice
[SC] StartService FAILED 5:

Access is denied.

C:\>netsh http add urlacl http://localhost:5000/ user=mini2012\svcAPI

URL reservation successfully added

C:\>sc start apiservice
[SC] StartService FAILED 5:

Access is denied.

5条回答
Explosion°爆炸
2楼-- · 2020-01-29 06:55

It looks like the problem was with the URL reservation. I didn't need one. If there is a URL reservation, it will just prevent the owin host from starting with the access denied error. Also, the default port for owin host is 5000. If there is a "dead" process that is still running on that port, it will block your service from starting. To check you can run netstat -a -b at the command prompt.

查看更多
相关推荐>>
3楼-- · 2020-01-29 06:56

For someone who is looking for a solution and didn't read the text.

The solution is to run visual studio as administrator.

查看更多
Deceive 欺骗
4楼-- · 2020-01-29 06:58

Run this command line under admin

netsh http add urlacl url=http://*:8080/ user=MyUser

查看更多
再贱就再见
5楼-- · 2020-01-29 07:14

Your service is running (most likely) under the LocalSystem (SYSTEM) account. This account is not in the Everyone security principal.

In short, to solve this, either make the namespace reservation for Anonymous Logon or change your service to run under the Network Service account which happens to be in the Everyone principal.

Third option is, of course, to create a new local/domain user, create the reservation for it and have the service run under this account. But then you'd have to worry about setting proper security permissions for it, so I'd go with one of the first two options.

查看更多
Fickle 薄情
6楼-- · 2020-01-29 07:17

Admin rights are not needed for port values of 5000 and higher

查看更多
登录 后发表回答