[英]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.