简体   繁体   English

来自服务的NATUPNPLib作为本地系统

[英]NATUPNPLib from a service as Local System

I have a windows service application that runs as Local System, and I want to be able to use NATUPNPLib to create port forwards for the (multiple and dynamic) ports it needs. 我有一个作为本地系统运行的Windows服务应用程序 ,我希望能够使用NATUPNPLib为所需的(多个和动态)端口创建端口转发。 I have been experimenting with various other UPnP Libraries that all fail, which is why when I tested NATUPNPLib and it worked in my test application I was pretty excited. 我一直在尝试用各种 其他的UPnP库,所有的失败,这就是为什么当我测试NATUPNPLib,它在我的测试应用程序的工作,我非常兴奋。

The problem comes when putting the same code into my service that is running under the local system account. 当将相同的代码放入在本地系统帐户下运行的我的服务中时,就会出现问题。 I always get the error The owner of the PerUser subscription is not logged on to the system specified (Exception from HRESULT: 0x80040210) . 我总是收到错误消息The owner of the PerUser subscription is not logged on to the system specified (Exception from HRESULT: 0x80040210) There is next to no documentation for this library or these errors (in fact, you need to generate the interops in VS2008 for .NET 3.5 as VS2010 always does them wrong). 此库几乎没有文档,也没有这些错误(实际上,您需要在VS2008 for .NET 3.5中生成互操作,因为VS2010总是会出错)。 What I have gathered from the error itself is that the Local System user doesn't have the rights to access UPnP. 我从错误本身中收集的信息是,本地系统用户无权访问UPnP。

Changing the user account the service runs as is non-trivial, aside from the fact that the app is auto-updating and designed to need very little configuration. 更改用户帐户后,该服务将像平常一样运行,除了该应用程序是自动更新的,而且只需很少的配置即可。

Has anyone got NATUPNPLib to work under Local System or found a decent UPnP library that I can use in C#? 有没有人让NATUPNPLib在本地系统下工作或找到了我可以在C#中使用的不错的UPnP库?

Here is the code I am using, couldn't really be much simpler! 这是我正在使用的代码,真的比这简单得多!

    /// <summary>
    /// Use NATUPNPLib to try and open a port forward
    /// </summary>
    /// <param name="intPortNumber">The port to open</param>
    /// <param name="strFriendlyName">Friendly name for the service</param>
    /// <returns>Boolean indicating whether the forward worked</returns>
    public static bool OpenUPnP(int intPortNumber, string strFriendlyName)
    {
        try {
            NATUPNPLib.UPnPNATClass upnpnat = new NATUPNPLib.UPnPNATClass();
            upnpnat.StaticPortMappingCollection.Add(intPortNumber, "TCP", intPortNumber, GetListenIP().ToString(), true, strFriendlyName);

            Database.AddLog("Opened port " + intPortNumber + " for " + strFriendlyName, "networking");
            return true;
        }
        catch(Exception e) {
            Database.AddLog("Unable foward port " + intPortNumber + " for " + strFriendlyName + ": Exception - " + e.Message, "networking", "warn");
            return false;
        }
    }

Which is part of Networking.cs 这是Networking.cs的一部分

Turns out, this is more a problem of implementation of UPnP on BT's Home Hub 3. With extended security on it returns this spurious error code to NATUPnPLib and without it on it still errors on certain ports. 事实证明,这更多是在BT的Home Hub 3上实现UPnP的问题。随着扩展的安全性,该虚假的错误代码将返回到NATUPnPLib,如果没有,它在某些端口上仍然会出错。

You can run UPnP commands under local system, as long as your router isn't a pile of poo. 您可以在本地系统下运行UPnP命令,只要路由器不是一堆便便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从服务运行msiexec(本地系统帐户) - Running msiexec from a service (Local System account) 从本地系统服务调用WTSEnumerateSessions - Calling WTSEnumerateSessions from a Local System service 使用来自一个 Windows 服务的本地系统和网络资源 - Using Local System and Network resources from one Windows Service C#权限问题导致无法从以本地系统帐户身份运行的Windows服务创建文件夹。 - C# Privilege issues creating folders from Windows service running as Local System account. 如何获取以用户身份运行的Windows 7应用程序的MainWindowHandle <foo> 从作为本地系统运行的服务中? - How can I get the MainWindowHandle of a Windows 7 application running as user <foo> from within a service running as Local System? 来自本地WCF服务的HttpWebRequest - HttpWebRequest from a local WCF service Windows 7操作系统中的服务不使用hosts文件作为本地服务 - Service in Windows 7 operating system not using the hosts file as local service 在作为本地系统运行的 windows 服务中进行键盘和鼠标操作? - Making keyboard and mouse operations in a windows service running as a local system? C#中的端口转发(与NATUPNPlib一起使用) - portforwarding in c# (matter with NATUPNPlib) Windows 服务作为本地系统帐户停留在“启动”状态 - Windows Service stuck on "starting" status as local system account
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM