convertersystems / opc-ua-client

Visualize and control your enterprise using OPC Unified Architecture (OPC UA) and Visual Studio.
MIT License
397 stars 115 forks source link

OpenAsync throws LocalPrivateKey is null #259

Closed warben0 closed 12 months ago

warben0 commented 12 months ago

Version: Workstation.UaClient.3.2.1

Hello, I have the following setup for my ClientSessionChannel:

var clientDescription = new ApplicationDescription
                        {
                            ApplicationName = "Workstation.UaClient.Testing",
                            ApplicationUri = $"urn:{System.Net.Dns.GetHostName()}:Workstation.UaClient.Testing",
                            ApplicationType = ApplicationType.Client
                        };
ClientSessionChannel client = new ClientSessionChannel(
                                                clientDescription,
                                                null, // no x509 certificates
                                                new AnonymousIdentity(), // no user identity, handle later
                                                url, 
                                                SecurityPolicyUris.Basic256);

Url in my situation is 192.168.120.85:4863, verified usable by uaexpert client using Basic256

when i execute client.OpenAsync(); it throws the following:

Workstation.ServiceModel.Ua.ServiceResultException: LocalPrivateKey is null.
   at Workstation.ServiceModel.Ua.Channels.UaSecureConversation.<SetRemoteCertificateAsync>d__72.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Workstation.ServiceModel.Ua.Channels.UaSecureConversationProvider.<CreateAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Workstation.ServiceModel.Ua.Channels.ClientSecureChannel.<OnOpenAsync>d__53.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Workstation.ServiceModel.Ua.Channels.ClientSessionChannel.<OnOpenAsync>d__62.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Workstation.ServiceModel.Ua.Channels.CommunicationObject.<OpenAsync>d__33.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Workstation.ServiceModel.Ua.Channels.CommunicationObject.<OpenAsync>d__33.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at VSTOTine.TineRibbon.<Scan>d__4.MoveNext() in C:\Users\warben\source\repos\VSTOTine\VSTOTine\TineRibbon.cs:line 97

not sure what "localprivatekey" its talking about, similar to #49 but not quite the same?

warben0 commented 12 months ago

This was actually an issue on my side, i added the certificateStore from the consoleapp example like this:

var certificateStore = new DirectoryStore(
                            Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Workstation.Excel", "pki"));

                        ClientSessionChannel client = new ClientSessionChannel(
                                                clientDescription,
                                                certificateStore,
                                                new AnonymousIdentity(), // no user identity, handle later
                                                url, 
                                                SecurityPolicyUris.Basic256);