Example #1
0
        public static void Main(string[] args)
        {
            //// To simple use the configuration stored within the XML configuration file
            //// beside the server application you just need to load the configuration file as the
            //// following code does demonstrate.
            //// By default it is not necessary to explicitly configure an OPC UA server. But in case
            //// of advanced and productive scenarios you will have to.

            // There are different ways to load the server configuration.
            OpcApplicationConfiguration configuration = null;

            // 1st Way: Load server config using a file path.
            configuration = OpcApplicationConfiguration.LoadServerConfigFile(
                Path.Combine(Environment.CurrentDirectory, "ServerConfig.xml"));

            // 2nd Way: Load server config specified in a specific section of your App.config.
            configuration = OpcApplicationConfiguration.LoadServerConfig("Opc.UaFx.Server");

            // If the server domain name does not match localhost just replace it
            // e.g. with the IP address or name of the server machine.
            var server = new OpcServer(
                "opc.tcp://localhost:4840/SampleServer",
                new SampleNodeManager());

            // To take use of the loaded server configuration, just set it on the server instance.
            server.Configuration = configuration;

            server.Start();
            server.Stop();

            // In case you are using the OPC UA server (Service) Application class, you can explicitly
            // trigger loading a configuration file using the App.config as the following code does
            // demonstrate.
            var app = new OpcServerApplication(
                "opc.tcp://localhost:4840/SampleServer",
                new SampleNodeManager());

            app.LoadConfiguration();

            // Alternatively you can assign the manually loaded server configuration on the server
            // instance used by the application instance, as the following code does demonstrate.
            app.Server.Configuration = configuration;

            app.Run();
        }
Example #2
0
        public static void Main(string[] args)
        {
            //// To simple use the in code configuration you just need to configure your client
            //// instance using the Configuration property of it.
            //// By default it is not necessary to explicitly configure an OPC UA client. But in case
            //// of advanced and productive scenarios you will have to.

            // If the server domain name does not match localhost just replace it
            // e.g. with the IP address or name of the server machine.
            var client = new OpcClient("opc.tcp://*****:*****@"%LocalApplicationData%\My Application\App Certificates";
            securityConfiguration.RejectedCertificateStore.StorePath
                = @"%LocalApplicationData%\My Application\Rejected Certificates";
            securityConfiguration.TrustedIssuerCertificates.StorePath
                = @"%LocalApplicationData%\My Application\Trusted Issuer Certificates";
            securityConfiguration.TrustedPeerCertificates.StorePath
                = @"%LocalApplicationData%\My Application\Trusted Peer Certificates";

            //// It is not necessary that all certificate stores have to point to the same root
            //// directory as above. Each store can also point to a totally different directory.

            client.Configuration = configuration;

            // 3rd Way: Directly change the default configuration of the client instance using the
            //         Configuration property.
            client.Configuration.ClientConfiguration.DefaultSessionTimeout = 300000; // 5 Minutes

            client.Connect();
            client.Disconnect();

            // In case you are using the OpcClientApplication class, you can directly configure
            // your client/application using the Configuration property of the application instance
            // as the following code does demonstrate.
            var app = new OpcClientApplication("opc.tcp://localhost:4840/SampleServer");

            app.Configuration.ClientConfiguration.DefaultSessionTimeout = 300000; // 5 Minutes

            app.Run();
        }
Example #3
0
        public RunOpc(ref EventLog log, ServiceConfig.OPC config)
        {
            _eventLog = log;



            _client.ApplicationName = AppDomain.CurrentDomain.FriendlyName;
            _client.ApplicationUri  = new Uri($"urn::{AppDomain.CurrentDomain.FriendlyName}");
            _client.SessionName     = "Server Monitoring";
            _client.ServerAddress   = new Uri($"opc.tcp:\\\\{config.ServerAddress}:{config.ServerPort}");



            if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Settings\\OpcConfiguration\\config.xml"))
            {
                if (config.Auth.AnonymousLogin != null && config.Auth.AnonymousLogin == false)
                {
                    try
                    {
                        // Default: ".\CertificateStores\Trusted"
                        _client.CertificateStores.ApplicationStore.Path
                            = AppDomain.CurrentDomain.BaseDirectory + "Settings\\App Certificates";
                        if (!Directory.Exists(_client.CertificateStores.ApplicationStore.Path))
                        {
                            Directory.CreateDirectory(_client.CertificateStores.ApplicationStore.Path);
                        }

                        // Default: ".\CertificateStores\Rejected"
                        _client.CertificateStores.RejectedStore.Path
                            = AppDomain.CurrentDomain.BaseDirectory + "Settings\\Rejected Certificates";
                        if (!Directory.Exists(_client.CertificateStores.RejectedStore.Path))
                        {
                            Directory.CreateDirectory(_client.CertificateStores.RejectedStore.Path);
                        }

                        // Default: ".\CertificateStores\Trusted"
                        _client.CertificateStores.TrustedIssuerStore.Path
                            = AppDomain.CurrentDomain.BaseDirectory + "Settings\\Trusted Issuer Certificates";
                        if (!Directory.Exists(_client.CertificateStores.TrustedIssuerStore.Path))
                        {
                            Directory.CreateDirectory(_client.CertificateStores.TrustedIssuerStore.Path);
                        }

                        // Default: ".\CertificateStores\Trusted"
                        _client.CertificateStores.TrustedPeerStore.Path
                            = AppDomain.CurrentDomain.BaseDirectory + "Settings\\Trusted Peer Certificates";
                        if (!Directory.Exists(_client.CertificateStores.TrustedPeerStore.Path))
                        {
                            Directory.CreateDirectory(_client.CertificateStores.TrustedPeerStore.Path);
                        }


                        var certificate = OpcCertificateManager.CreateCertificate(_client);
                        _client.CertificateStores.ApplicationStore.Add(certificate);
                        _client.Certificate = certificate;
                        OpcCertificateManager.SaveCertificate("Certificate", certificate);

                        _client.Security.UserIdentity   = new OpcClientIdentity(config.Auth.Username, config.Auth.Password);
                        _client.Security.EndpointPolicy = new OpcSecurityPolicy(OpcSecurityMode.SignAndEncrypt, OpcSecurityAlgorithm.Auto);
                        _client.Security.VerifyServersCertificateDomains = true;
                        _client.Security.AutoAcceptUntrustedCertificates = true;

                        _client.CertificateValidationFailed += ClientOnCertificateValidationFailed;
                    }
                    catch (Exception e)
                    {
                        _eventLog.WriteEntry($"Error with OPC Certification.\n{e.Message}\n\n{e.InnerException}\n\n{e.StackTrace}", EventLogEntryType.Error, (int)EventIds.Ids.OpcCertificateError);
                        Environment.Exit(1);
                    }
                }
                else
                {
                    _client.Security.AutoAcceptUntrustedCertificates = true;
                }

                _client.Configuration.ApplicationName      = _client.ApplicationName;
                _client.Configuration.ApplicationType      = ApplicationType.Client;
                _client.Configuration.ApplicationUri       = _client.ApplicationUri.AbsoluteUri;
                _client.Configuration.CertificateValidator = new CertificateValidator();
                _client.Configuration.ClientConfiguration.DefaultSessionTimeout = 10;
                _client.Configuration.ProductUri = _client.ApplicationName;
                _client.Configuration.Validate();
                if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Settings\\OpcConfiguration"))
                {
                    Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "Settings\\OpcConfiguration");
                }
                _client.Configuration.SaveToFile(AppDomain.CurrentDomain.BaseDirectory + "Settings\\OpcConfiguration\\config.xml");
            }
            else
            {
                _client.Configuration = OpcApplicationConfiguration.LoadClientConfigFile(AppDomain.CurrentDomain.BaseDirectory + "Settings\\OpcConfiguration\\config.xml");
                if (config.Auth.AnonymousLogin != null && config.Auth.AnonymousLogin == false)
                {
                    try
                    {
                        // Default: ".\CertificateStores\Trusted"
                        _client.CertificateStores.ApplicationStore.Path
                            = AppDomain.CurrentDomain.BaseDirectory + "Settings\\App Certificates";


                        var certPath = _client.CertificateStores.ApplicationStore.Path +
                                       $"\\private\\{_client.ApplicationName} [{_client.Configuration.SecurityConfiguration.ApplicationCertificate.Thumbprint}].pfx";


                        _client.Certificate = OpcCertificateManager.LoadCertificate(certPath);
                        _client.CertificateStores.RejectedStore.GetCertificates();
                        _client.CertificateStores.TrustedIssuerStore.GetCertificates();
                        _client.CertificateStores.TrustedPeerStore.GetCertificates();


                        _client.Security.UserIdentity   = new OpcClientIdentity(config.Auth.Username, config.Auth.Password);
                        _client.Security.EndpointPolicy = new OpcSecurityPolicy(OpcSecurityMode.SignAndEncrypt, OpcSecurityAlgorithm.Auto);
                        _client.Security.VerifyServersCertificateDomains = true;
                        _client.Security.AutoAcceptUntrustedCertificates = true;

                        _client.CertificateValidationFailed += ClientOnCertificateValidationFailed;
                    }
                    catch (Exception e)
                    {
                        _eventLog.WriteEntry($"Error with OPC Certification.\n{e.Message}\n\n{e.StackTrace}\n\n{e.InnerException}", EventLogEntryType.Error, (int)EventIds.Ids.OpcCertificateError);
                        Environment.Exit(1);
                    }
                }
                else
                {
                    _client.Security.AutoAcceptUntrustedCertificates = true;
                }
            }

            _client.Connect();
        }