From 43b428a1cd3ccea9fd45cb0cd133b311c210e522 Mon Sep 17 00:00:00 2001 From: Peter Kurhajec <61538034+PTKu@users.noreply.github.com> Date: Wed, 10 Apr 2024 06:29:21 +0200 Subject: [PATCH] The most significant changes involve the addition of a new parameter `ignoreSslErrors` to the `WebApiConnector` class, `CreateWebApi` method, and `WebApiConnectorFactory` class. This parameter allows the user to choose whether to ignore SSL errors or not. (#300) 1. The `WebApiConnector` class in `WebApiConnector.cs` has been updated to include a new parameter `ignoreSSLErros` in its constructor. This parameter is a boolean that, when set to true, will ignore SSL errors. If `ignoreSSLErros` is set to true, a lambda function is set to the `CertificateCallback` of `ServerCertificateCallback` that always returns true, effectively ignoring any SSL errors. 2. The `CreateWebApi` method in `WebApiConnectorExtensions.cs` has been updated to include a new parameter `ignoreSslErrors` with a default value of false. The `Parameters` property of the `ConnectorAdapter` object returned by the method has been updated to include `ignoreSslErrors`. 3. The `WebApiConnectorFactory` class in `WebApiConnectorFactory.cs` has been updated to handle the new `ignoreSslErrors` parameter. The `WebApiConnector` object returned by the `Create` method now includes `ignoreSslErrors` as a parameter. --- .../src/AXSharp.Connector.S71500.WebAPI/WebApiConnector.cs | 6 ++++++ .../WebApiConnectorExtensions.cs | 4 +++- .../WebApiConnectorFactory.cs | 5 +++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnector.cs b/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnector.cs index 17c494b8..54372d45 100644 --- a/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnector.cs +++ b/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnector.cs @@ -50,9 +50,11 @@ public class WebApiConnector : Connector /// User name. /// Password. /// Customized server certificate handler. + /// When set to true ssl error are ignored. /// Root DB name (AX uses 'TGlobalVariablesDB') public WebApiConnector(string ipAddress, string userName, string password, Func? customServerCertHandler, + bool ignoreSSLErros, eTargetProjectPlatform platform = eTargetProjectPlatform.SIMATICAX, string dbName = "\"TGlobalVariablesDB\"") { @@ -62,6 +64,10 @@ public WebApiConnector(string ipAddress, string userName, string password, UserName = userName; UserPassword = password; + if (ignoreSSLErros) + ServerCertificateCallback.CertificateCallback = + (sender, cert, chain, sslPolicyErrors) => true; + var serviceFactory = new ApiStandardServiceFactory(); var client = serviceFactory.GetHttpClient(ipAddress, UserName, UserPassword); requestHandler = new ApiHttpClientRequestHandler(client, diff --git a/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnectorExtensions.cs b/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnectorExtensions.cs index a58f9f66..94b4c439 100644 --- a/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnectorExtensions.cs +++ b/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnectorExtensions.cs @@ -43,16 +43,18 @@ public static ConnectorAdapter CreateWebApi(this ConnectorAdapterBuilder adapter /// User name. /// Password. /// Customized server certificate handler. + /// When set to true ssl errors are ignored /// Name of default DB. The DB used to store all data in an AX project is 'TGlobalVariablesDB'. /// Connector adapter for WebAPI connection. public static ConnectorAdapter CreateWebApi(this ConnectorAdapterBuilder adapter, string ipAddress, string userName, string password, Func? customServerCertHandler, + bool ignoreSslErrors = false, eTargetProjectPlatform platform = eTargetProjectPlatform.SIMATICAX, string dbName = "\"TGlobalVariablesDB\"") { return new ConnectorAdapter(typeof(WebApiConnectorFactory)) - { Parameters = new object[] { ipAddress, userName, password, customServerCertHandler, platform, dbName } }; + { Parameters = new object[] { ipAddress, userName, password, customServerCertHandler, ignoreSslErrors, platform, dbName } }; } public static DateOnly AdjustForLeapDate(this long value) diff --git a/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnectorFactory.cs b/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnectorFactory.cs index 0e2c646c..737a75c2 100644 --- a/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnectorFactory.cs +++ b/src/AXSharp.connectors/src/AXSharp.Connector.S71500.WebAPI/WebApiConnectorFactory.cs @@ -32,8 +32,9 @@ public override Connector CreateConnector(object[] parameters) (string)parameters[1], (string)parameters[2], (Func)parameters[3], - (eTargetProjectPlatform)parameters[4], - (string)parameters[5]); + (bool)parameters[4], + (eTargetProjectPlatform)parameters[5], + (string)parameters[6]); } }