Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to connect using a custom endpoint #2905

Open
2 of 5 tasks
deakintsang opened this issue Dec 12, 2024 · 0 comments
Open
2 of 5 tasks

How to connect using a custom endpoint #2905

deakintsang opened this issue Dec 12, 2024 · 0 comments
Labels
question The issue contains a question. After no activity the issue might be closed.

Comments

@deakintsang
Copy link

deakintsang commented Dec 12, 2024

Type of issue

  • Bug
  • Enhancement
  • Compliance
  • Question
  • Help wanted

Current Behavior

I know CoreClientUtils SelectEndpoint can return the safest EndpointDescription, but I want to create a EndpointDescription, testing different Endpoint security policy, but it can't work;

I found CoreClientUtils SelectEndpoint the certificate is not the same as I create EndpointDescription certificate, unable to realize the connection;

I trust a certificate on the server side, can't I connect with this client certificate?


 private EndpointDescription CreateEndpointDescription(ApplicationConfiguration configuration, string url, string secPolicy, MessageSecurityMode msgSecMode)
        {
            EndpointDescription endpointDescription = new EndpointDescription();
            endpointDescription.EndpointUrl = url;
            endpointDescription.SecurityPolicyUri = SecurityPolicies.GetUri(secPolicy);
            endpointDescription.SecurityMode = msgSecMode;
            endpointDescription.SecurityLevel = ServerSecurityPolicy.CalculateSecurityLevel(msgSecMode, SecurityPolicies.GetUri(secPolicy));

            var tokenPolicy = new UserTokenPolicy(UserTokenType.UserName);
            tokenPolicy.SecurityPolicyUri = SecurityPolicies.GetUri(secPolicy);
            endpointDescription.UserIdentityTokens.Add(tokenPolicy);
            endpointDescription.TransportProfileUri = Profiles.UaTcpTransport;

            if (secPolicy != SecurityPolicies.None)
            {
                var InstanceCertificate = configuration.SecurityConfiguration.ApplicationCertificate.Find(true).Result;
                endpointDescription.ServerCertificate = InstanceCertificate.RawData;
            }

            return endpointDescription;
        }


        public async void Connect(string url, string secPolicy, MessageSecurityMode msgSecMode, bool userAuth, string userName, string password)
        {
            try
            {
                //Secify application configuration
                ApplicationConfiguration ApplicationConfig = mApplicationConfig;

                //Create EndPoint description
                EndpointDescription EndpointDescription = CreateEndpointDescription(ApplicationConfig, url, secPolicy, MessageSecurityMode.SignAndEncrypt);

                //Create EndPoint configuration
                EndpointConfiguration EndpointConfiguration = EndpointConfiguration.Create(ApplicationConfig);

                //Create an Endpoint object to connect to server
                ConfiguredEndpoint Endpoint = new ConfiguredEndpoint(null, EndpointDescription, EndpointConfiguration);


                //Create anonymous user identity
                UserIdentity UserIdentity;
                if (userAuth)
                {
                    UserIdentity = new UserIdentity(userName,password);
                }
                else
                {
                    UserIdentity = new UserIdentity();
                }

                ApplicationConfig.CertificateValidator.Update(ApplicationConfig);

                mSession = await Session.Create(
                mApplicationConfig,
                Endpoint,
                false,
                false,
                ApplicationConfig.ApplicationName,
                6000,
                UserIdentity,
                new string[] { });

            }
            catch (Exception e)
            {
                //handle Exception here
                
            }
        }

Expected Behavior

No response

Steps To Reproduce

No response

Environment

OS:window10
Environment: Visual Studio 2022
Runtime: .NET 8.0
Nuget Version:OPCFoundation.NetStandard.Opc.Ua(1.5.374.126)
Server: Prosys OPC UA Simulation Server

Anything else?

No response

@mregen mregen changed the title <title> How to connect using a custom endpoint Dec 13, 2024
@mregen mregen added the question The issue contains a question. After no activity the issue might be closed. label Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question The issue contains a question. After no activity the issue might be closed.
Projects
None yet
Development

No branches or pull requests

2 participants