I have been stuck now for about 5 days with this problem now.

Basically I'm trying to connect to an OPC server (Kepware) from a remote pc. The code I have written works if I run it on the server where Kepware is installed (in this example 192.168.102.104).

However if I run it from a remote PC I get the following error.

System.UnauthorizedAccesException: Access is denied 
(Error from HRESULT: 0x080070005 E_ACCESSIFDENIED))

I have setup the server following these instructions: http://www.kepware.com/Support_Center/SupportDocuments/Remote%20OPC%20DA%20-%20Quick%20Start%20Guide%20(DCOM).pdf

The PCs are not in a domain and both PCs contain the user Administrator with the same password.

Could anyone tell me if the problem might be with the PCs setup or my Code

CODE::

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();
    }
}

Unmanaged Class::

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
    }
}

The impersonation class just impersonates the local user Administrator.

When I run this on the server with Kepware I get the output - Kepware.KepServerEx.V5 When I run it on the client PC I get the above error

Any help would be greatly greatly appreciated. Thanks.

有帮助吗?

解决方案

Found the source of the problem.

Why is it always the way that as soon as I post I figure it out?

The problem was with how the server was set up. I won't bore you with all the details here but it was to do with the identity in the DCOM settings (in DCONcnfg) being set to the wrong user.

Apparently I ignored that bit of the server set up instructions.

Sorry for wasting anyone's time.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top