我现在已经被这个问题困住了大约 5 天。
基本上我正在尝试从远程 PC 连接到 OPC 服务器(Kepware)。如果我在安装了 Kepware 的服务器(在本例中为 192.168.102.104)上运行我编写的代码,则它可以工作。
但是,如果我从远程 PC 运行它,我会收到以下错误。
System.UnauthorizedAccesException: Access is denied
(Error from HRESULT: 0x080070005 E_ACCESSIFDENIED))
我已按照以下说明设置服务器: http ://www.kepware.com/Support_Center/SupportDocuments/Remote%20OPC%20DA%20-%20Quick%20Start%20Guide%20(DCOM).pdf
PC 不在域中,并且两台 PC 都包含具有相同密码的用户 Administrator。
谁能告诉我问题可能出在 PC 设置或我的代码上
代码::
class Program
{
static void Main(string[] args)
{
try
{
Unmanaged.CoInitializeSecurity(IntPtr.Zero, -1, IntPtr.Zero, IntPtr.Zero,
Unmanaged.RpcAuthnLevel.Connect,
Unmanaged.RpcImpLevel.Anonymous, IntPtr.Zero,
Unmanaged.EoAuthnCap.None, IntPtr.Zero);
using (new Impersonation("Administrator", "TEST", "Password"))
{
OPCServer opcServer = new OPCServer();
opcServer.Connect("Kepware.KEPServerEx.V5", "192.168.102.104");
Console.WriteLine(opcServer.ServerName);
}
}
catch (Exception ex)
{
Console.WriteLine("Connection error:::\n\n{0}\n\n",ex);
Console.ReadLine();
}
Console.ReadLine();
}
}
非托管类::
partial class Unmanaged
{
[DllImport("ole32.dll")]
public static extern int CoInitializeSecurity(IntPtr pVoid, int
cAuthSvc, IntPtr asAuthSvc, IntPtr pReserved1, RpcAuthnLevel level,
RpcImpLevel impers, IntPtr pAuthList, EoAuthnCap dwCapabilities, IntPtr
pReserved3);
public enum RpcAuthnLevel
{
Default = 0,
None = 1,
Connect = 2,
Call = 3,
Pkt = 4,
PktIntegrity = 5,
PktPrivacy = 6
}
public enum RpcImpLevel
{
Default = 0,
Anonymous = 1,
Identify = 2,
Impersonate = 3,
Delegate = 4
}
public enum EoAuthnCap
{
None = 0x00,
MutualAuth = 0x01,
StaticCloaking = 0x20,
DynamicCloaking = 0x40,
AnyAuthority = 0x80,
MakeFullSIC = 0x100,
Default = 0x800,
SecureRefs = 0x02,
AccessControl = 0x04,
AppID = 0x08,
Dynamic = 0x10,
RequireFullSIC = 0x200,
AutoImpersonate = 0x400,
NoCustomMarshal = 0x2000,
DisableAAA = 0x1000
}
}
模拟类只是模拟本地用户管理员。
当我使用 Kepware 在服务器上运行它时,我得到了输出 - Kepware.KepServerEx.V5 当我在客户端 PC 上运行它时,我得到了上述错误
任何帮助将不胜感激。谢谢。