From 3df0cd76ffd210f5b0e9c0dcc7d6d562f8b41d11 Mon Sep 17 00:00:00 2001 From: "Nagabalaji Siddaiah Buddala -X (nsiddaia - XORIANT SOLUTIONS PRIVATE LIMITED at Cisco)" Date: Tue, 24 Nov 2026 12:23:58 +0530 Subject: [PATCH 1/4] updating httpclient logic to adapt skip certs --- .../csharp/libraries/httpclient/ApiClient.mustache | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache index 1cbab2bfb4c2..1db12a2fe8c4 100644 --- a/modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache @@ -510,6 +510,18 @@ namespace {{packageName}}.Client _httpClientHandler.ClientCertificates.AddRange(configuration.ClientCertificates); } + if (configuration.RemoteCertificateValidationCallback != null) + { + if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor."); + // HttpClientHandler properties are frozen after the first request, so only assign when not already set. + if (_httpClientHandler.ServerCertificateCustomValidationCallback == null) + { + _httpClientHandler.ServerCertificateCustomValidationCallback = + (request, certificate, chain, sslPolicyErrors) => + configuration.RemoteCertificateValidationCallback(request, certificate, chain, sslPolicyErrors); + } + } + {{^net60OrLater}} var cookieContainer = req.Properties.ContainsKey("CookieContainer") ? req.Properties["CookieContainer"] as List : null; {{/net60OrLater}} From 5a5b9ffcda9cb2ebfaf1d325b5aea171e56cbffb Mon Sep 17 00:00:00 2001 From: "Nagabalaji Siddaiah Buddala -X (nsiddaia - XORIANT SOLUTIONS PRIVATE LIMITED at Cisco)" Date: Thu, 26 Nov 2026 14:52:52 +0530 Subject: [PATCH 2/4] Generated samples --- .../src/Org.OpenAPITools/Client/ApiClient.cs | 12 ++++++++++++ .../src/Org.OpenAPITools/Client/ApiClient.cs | 12 ++++++++++++ .../src/Org.OpenAPITools/Client/ApiClient.cs | 12 ++++++++++++ .../src/Org.OpenAPITools/Client/ApiClient.cs | 12 ++++++++++++ .../src/Org.OpenAPITools/Client/ApiClient.cs | 12 ++++++++++++ 5 files changed, 60 insertions(+) diff --git a/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs index 01ed136022b0..12ed592de411 100644 --- a/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs @@ -505,6 +505,18 @@ private async Task> ExecAsync(HttpRequestMessage req, _httpClientHandler.ClientCertificates.AddRange(configuration.ClientCertificates); } + if (configuration.RemoteCertificateValidationCallback != null) + { + if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor."); + // HttpClientHandler properties are frozen after the first request, so only assign when not already set. + if (_httpClientHandler.ServerCertificateCustomValidationCallback == null) + { + _httpClientHandler.ServerCertificateCustomValidationCallback = + (request, certificate, chain, sslPolicyErrors) => + configuration.RemoteCertificateValidationCallback(request, certificate, chain, sslPolicyErrors); + } + } + if (req.Options.TryGetValue(_httpOptionsCookieContainerKey, out var cookieContainer)) { diff --git a/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs index 1e3edc1fec0f..deca9fb5e07d 100644 --- a/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -505,6 +505,18 @@ private async Task> ExecAsync(HttpRequestMessage req, _httpClientHandler.ClientCertificates.AddRange(configuration.ClientCertificates); } + if (configuration.RemoteCertificateValidationCallback != null) + { + if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor."); + // HttpClientHandler properties are frozen after the first request, so only assign when not already set. + if (_httpClientHandler.ServerCertificateCustomValidationCallback == null) + { + _httpClientHandler.ServerCertificateCustomValidationCallback = + (request, certificate, chain, sslPolicyErrors) => + configuration.RemoteCertificateValidationCallback(request, certificate, chain, sslPolicyErrors); + } + } + if (req.Options.TryGetValue(_httpOptionsCookieContainerKey, out var cookieContainer)) { diff --git a/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs index 01ed136022b0..12ed592de411 100644 --- a/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs @@ -505,6 +505,18 @@ private async Task> ExecAsync(HttpRequestMessage req, _httpClientHandler.ClientCertificates.AddRange(configuration.ClientCertificates); } + if (configuration.RemoteCertificateValidationCallback != null) + { + if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor."); + // HttpClientHandler properties are frozen after the first request, so only assign when not already set. + if (_httpClientHandler.ServerCertificateCustomValidationCallback == null) + { + _httpClientHandler.ServerCertificateCustomValidationCallback = + (request, certificate, chain, sslPolicyErrors) => + configuration.RemoteCertificateValidationCallback(request, certificate, chain, sslPolicyErrors); + } + } + if (req.Options.TryGetValue(_httpOptionsCookieContainerKey, out var cookieContainer)) { diff --git a/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs index 1e3edc1fec0f..deca9fb5e07d 100644 --- a/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -505,6 +505,18 @@ private async Task> ExecAsync(HttpRequestMessage req, _httpClientHandler.ClientCertificates.AddRange(configuration.ClientCertificates); } + if (configuration.RemoteCertificateValidationCallback != null) + { + if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor."); + // HttpClientHandler properties are frozen after the first request, so only assign when not already set. + if (_httpClientHandler.ServerCertificateCustomValidationCallback == null) + { + _httpClientHandler.ServerCertificateCustomValidationCallback = + (request, certificate, chain, sslPolicyErrors) => + configuration.RemoteCertificateValidationCallback(request, certificate, chain, sslPolicyErrors); + } + } + if (req.Options.TryGetValue(_httpOptionsCookieContainerKey, out var cookieContainer)) { diff --git a/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs index f6913dca419e..58a71fd45fc3 100644 --- a/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -503,6 +503,18 @@ private async Task> ExecAsync(HttpRequestMessage req, _httpClientHandler.ClientCertificates.AddRange(configuration.ClientCertificates); } + if (configuration.RemoteCertificateValidationCallback != null) + { + if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor."); + // HttpClientHandler properties are frozen after the first request, so only assign when not already set. + if (_httpClientHandler.ServerCertificateCustomValidationCallback == null) + { + _httpClientHandler.ServerCertificateCustomValidationCallback = + (request, certificate, chain, sslPolicyErrors) => + configuration.RemoteCertificateValidationCallback(request, certificate, chain, sslPolicyErrors); + } + } + var cookieContainer = req.Properties.ContainsKey("CookieContainer") ? req.Properties["CookieContainer"] as List : null; if (cookieContainer != null) From 658ae448c9b1d98b3182f6178b0f55d1b5c24802 Mon Sep 17 00:00:00 2001 From: "Nagabalaji Siddaiah Buddala -X (nsiddaia - XORIANT SOLUTIONS PRIVATE LIMITED at Cisco)" Date: Thu, 26 Nov 2026 17:34:43 +0530 Subject: [PATCH 3/4] Generated samples --- .../libraries/httpclient/ApiClient.mustache | 51 ++++++++++++++++--- .../src/Org.OpenAPITools/Client/ApiClient.cs | 51 ++++++++++++++++--- .../src/Org.OpenAPITools/Client/ApiClient.cs | 51 ++++++++++++++++--- .../src/Org.OpenAPITools/Client/ApiClient.cs | 51 ++++++++++++++++--- .../src/Org.OpenAPITools/Client/ApiClient.cs | 51 ++++++++++++++++--- .../src/Org.OpenAPITools/Client/ApiClient.cs | 51 ++++++++++++++++--- 6 files changed, 264 insertions(+), 42 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache index 1db12a2fe8c4..5bd5141d8130 100644 --- a/modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache @@ -200,6 +200,13 @@ namespace {{packageName}}.Client private readonly HttpClientHandler _httpClientHandler; private readonly HttpClient _httpClient; private readonly bool _disposeClient; +#if !NET47 + // The callback most recently requested through Configuration.RemoteCertificateValidationCallback. + // The delegate installed on the HttpClientHandler reads this field, so it is refreshed on every request. + private volatile System.Net.Security.RemoteCertificateValidationCallback _remoteCertificateValidationCallback; + // 0 = not installed yet, 1 = installed (guarded with Interlocked for thread safety). + private int _remoteCertificateValidationInstalled; +#endif /// /// Specifies the settings on a object. @@ -253,7 +260,9 @@ namespace {{packageName}}.Client /// /// /// Some configuration settings will not be applied without passing an HttpClientHandler. - /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. + /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings, Remote Certificate Validation Callback. + /// Handler-level settings (Proxy, Client Certificates, Remote Certificate Validation Callback) must be configured before the first request is sent, + /// because HttpClientHandler properties cannot be changed once it has started sending requests. /// public ApiClient(HttpClient client, HttpClientHandler handler = null) : this(client, {{packageName}}.Client.GlobalConfiguration.Instance.BasePath, handler) @@ -270,7 +279,9 @@ namespace {{packageName}}.Client /// /// /// Some configuration settings will not be applied without passing an HttpClientHandler. - /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. + /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings, Remote Certificate Validation Callback. + /// Handler-level settings (Proxy, Client Certificates, Remote Certificate Validation Callback) must be configured before the first request is sent, + /// because HttpClientHandler properties cannot be changed once it has started sending requests. /// public ApiClient(HttpClient client, string basePath, HttpClientHandler handler = null) { @@ -513,13 +524,39 @@ namespace {{packageName}}.Client if (configuration.RemoteCertificateValidationCallback != null) { if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor."); - // HttpClientHandler properties are frozen after the first request, so only assign when not already set. - if (_httpClientHandler.ServerCertificateCustomValidationCallback == null) +#if NET47 + // HttpClientHandler.ServerCertificateCustomValidationCallback is only available from .NET Framework 4.7.1. + throw new NotSupportedException("Configuration `RemoteCertificateValidationCallback` is not supported when targeting .NET Framework 4.7, because HttpClientHandler.ServerCertificateCustomValidationCallback requires .NET Framework 4.7.1 or later."); +#else + // Refresh the callback on every request; the delegate installed below reads this field, + // so a later request (or a replaced Configuration) is not stuck with the first callback. + _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback; + // Install the handler delegate only once. HttpClientHandler properties are frozen after the + // first request, so the callback must be configured before this client sends its first request + // (same limitation as `Proxy` and `ClientCertificates`). + if (Interlocked.CompareExchange(ref _remoteCertificateValidationInstalled, 1, 0) == 0 + && _httpClientHandler.ServerCertificateCustomValidationCallback == null) { - _httpClientHandler.ServerCertificateCustomValidationCallback = - (request, certificate, chain, sslPolicyErrors) => - configuration.RemoteCertificateValidationCallback(request, certificate, chain, sslPolicyErrors); + try + { + _httpClientHandler.ServerCertificateCustomValidationCallback = + (request, certificate, chain, sslPolicyErrors) => + { + var callback = _remoteCertificateValidationCallback; + // Fall back to the default policy if the callback was cleared after installation. + return callback != null + ? callback(request, certificate, chain, sslPolicyErrors) + : sslPolicyErrors == System.Net.Security.SslPolicyErrors.None; + }; + } + catch (InvalidOperationException ex) + { + // Allow the failure to be reported again instead of silently skipping validation later. + Interlocked.Exchange(ref _remoteCertificateValidationInstalled, 0); + throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` must be set before the first request is sent: the HttpClientHandler has already started sending requests and its properties can no longer be changed.", ex); + } } +#endif } {{^net60OrLater}} diff --git a/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs index 12ed592de411..142a13358d89 100644 --- a/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs @@ -202,6 +202,13 @@ internal partial class ApiClient : IDisposable, ISynchronousClient, IAsynchronou private readonly HttpClientHandler _httpClientHandler; private readonly HttpClient _httpClient; private readonly bool _disposeClient; +#if !NET47 + // The callback most recently requested through Configuration.RemoteCertificateValidationCallback. + // The delegate installed on the HttpClientHandler reads this field, so it is refreshed on every request. + private volatile System.Net.Security.RemoteCertificateValidationCallback _remoteCertificateValidationCallback; + // 0 = not installed yet, 1 = installed (guarded with Interlocked for thread safety). + private int _remoteCertificateValidationInstalled; +#endif /// /// Specifies the settings on a object. @@ -255,7 +262,9 @@ public ApiClient(string basePath) /// /// /// Some configuration settings will not be applied without passing an HttpClientHandler. - /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. + /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings, Remote Certificate Validation Callback. + /// Handler-level settings (Proxy, Client Certificates, Remote Certificate Validation Callback) must be configured before the first request is sent, + /// because HttpClientHandler properties cannot be changed once it has started sending requests. /// public ApiClient(HttpClient client, HttpClientHandler handler = null) : this(client, Org.OpenAPITools.Client.GlobalConfiguration.Instance.BasePath, handler) @@ -272,7 +281,9 @@ public ApiClient(HttpClient client, HttpClientHandler handler = null) : /// /// /// Some configuration settings will not be applied without passing an HttpClientHandler. - /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. + /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings, Remote Certificate Validation Callback. + /// Handler-level settings (Proxy, Client Certificates, Remote Certificate Validation Callback) must be configured before the first request is sent, + /// because HttpClientHandler properties cannot be changed once it has started sending requests. /// public ApiClient(HttpClient client, string basePath, HttpClientHandler handler = null) { @@ -508,13 +519,39 @@ private async Task> ExecAsync(HttpRequestMessage req, if (configuration.RemoteCertificateValidationCallback != null) { if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor."); - // HttpClientHandler properties are frozen after the first request, so only assign when not already set. - if (_httpClientHandler.ServerCertificateCustomValidationCallback == null) +#if NET47 + // HttpClientHandler.ServerCertificateCustomValidationCallback is only available from .NET Framework 4.7.1. + throw new NotSupportedException("Configuration `RemoteCertificateValidationCallback` is not supported when targeting .NET Framework 4.7, because HttpClientHandler.ServerCertificateCustomValidationCallback requires .NET Framework 4.7.1 or later."); +#else + // Refresh the callback on every request; the delegate installed below reads this field, + // so a later request (or a replaced Configuration) is not stuck with the first callback. + _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback; + // Install the handler delegate only once. HttpClientHandler properties are frozen after the + // first request, so the callback must be configured before this client sends its first request + // (same limitation as `Proxy` and `ClientCertificates`). + if (Interlocked.CompareExchange(ref _remoteCertificateValidationInstalled, 1, 0) == 0 + && _httpClientHandler.ServerCertificateCustomValidationCallback == null) { - _httpClientHandler.ServerCertificateCustomValidationCallback = - (request, certificate, chain, sslPolicyErrors) => - configuration.RemoteCertificateValidationCallback(request, certificate, chain, sslPolicyErrors); + try + { + _httpClientHandler.ServerCertificateCustomValidationCallback = + (request, certificate, chain, sslPolicyErrors) => + { + var callback = _remoteCertificateValidationCallback; + // Fall back to the default policy if the callback was cleared after installation. + return callback != null + ? callback(request, certificate, chain, sslPolicyErrors) + : sslPolicyErrors == System.Net.Security.SslPolicyErrors.None; + }; + } + catch (InvalidOperationException ex) + { + // Allow the failure to be reported again instead of silently skipping validation later. + Interlocked.Exchange(ref _remoteCertificateValidationInstalled, 0); + throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` must be set before the first request is sent: the HttpClientHandler has already started sending requests and its properties can no longer be changed.", ex); + } } +#endif } diff --git a/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs index deca9fb5e07d..497c2b01405d 100644 --- a/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -202,6 +202,13 @@ public partial class ApiClient : IDisposable, ISynchronousClient, IAsynchronousC private readonly HttpClientHandler _httpClientHandler; private readonly HttpClient _httpClient; private readonly bool _disposeClient; +#if !NET47 + // The callback most recently requested through Configuration.RemoteCertificateValidationCallback. + // The delegate installed on the HttpClientHandler reads this field, so it is refreshed on every request. + private volatile System.Net.Security.RemoteCertificateValidationCallback _remoteCertificateValidationCallback; + // 0 = not installed yet, 1 = installed (guarded with Interlocked for thread safety). + private int _remoteCertificateValidationInstalled; +#endif /// /// Specifies the settings on a object. @@ -255,7 +262,9 @@ public ApiClient(string basePath) /// /// /// Some configuration settings will not be applied without passing an HttpClientHandler. - /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. + /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings, Remote Certificate Validation Callback. + /// Handler-level settings (Proxy, Client Certificates, Remote Certificate Validation Callback) must be configured before the first request is sent, + /// because HttpClientHandler properties cannot be changed once it has started sending requests. /// public ApiClient(HttpClient client, HttpClientHandler handler = null) : this(client, Org.OpenAPITools.Client.GlobalConfiguration.Instance.BasePath, handler) @@ -272,7 +281,9 @@ public ApiClient(HttpClient client, HttpClientHandler handler = null) : /// /// /// Some configuration settings will not be applied without passing an HttpClientHandler. - /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. + /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings, Remote Certificate Validation Callback. + /// Handler-level settings (Proxy, Client Certificates, Remote Certificate Validation Callback) must be configured before the first request is sent, + /// because HttpClientHandler properties cannot be changed once it has started sending requests. /// public ApiClient(HttpClient client, string basePath, HttpClientHandler handler = null) { @@ -508,13 +519,39 @@ private async Task> ExecAsync(HttpRequestMessage req, if (configuration.RemoteCertificateValidationCallback != null) { if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor."); - // HttpClientHandler properties are frozen after the first request, so only assign when not already set. - if (_httpClientHandler.ServerCertificateCustomValidationCallback == null) +#if NET47 + // HttpClientHandler.ServerCertificateCustomValidationCallback is only available from .NET Framework 4.7.1. + throw new NotSupportedException("Configuration `RemoteCertificateValidationCallback` is not supported when targeting .NET Framework 4.7, because HttpClientHandler.ServerCertificateCustomValidationCallback requires .NET Framework 4.7.1 or later."); +#else + // Refresh the callback on every request; the delegate installed below reads this field, + // so a later request (or a replaced Configuration) is not stuck with the first callback. + _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback; + // Install the handler delegate only once. HttpClientHandler properties are frozen after the + // first request, so the callback must be configured before this client sends its first request + // (same limitation as `Proxy` and `ClientCertificates`). + if (Interlocked.CompareExchange(ref _remoteCertificateValidationInstalled, 1, 0) == 0 + && _httpClientHandler.ServerCertificateCustomValidationCallback == null) { - _httpClientHandler.ServerCertificateCustomValidationCallback = - (request, certificate, chain, sslPolicyErrors) => - configuration.RemoteCertificateValidationCallback(request, certificate, chain, sslPolicyErrors); + try + { + _httpClientHandler.ServerCertificateCustomValidationCallback = + (request, certificate, chain, sslPolicyErrors) => + { + var callback = _remoteCertificateValidationCallback; + // Fall back to the default policy if the callback was cleared after installation. + return callback != null + ? callback(request, certificate, chain, sslPolicyErrors) + : sslPolicyErrors == System.Net.Security.SslPolicyErrors.None; + }; + } + catch (InvalidOperationException ex) + { + // Allow the failure to be reported again instead of silently skipping validation later. + Interlocked.Exchange(ref _remoteCertificateValidationInstalled, 0); + throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` must be set before the first request is sent: the HttpClientHandler has already started sending requests and its properties can no longer be changed.", ex); + } } +#endif } diff --git a/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs index 12ed592de411..142a13358d89 100644 --- a/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs @@ -202,6 +202,13 @@ internal partial class ApiClient : IDisposable, ISynchronousClient, IAsynchronou private readonly HttpClientHandler _httpClientHandler; private readonly HttpClient _httpClient; private readonly bool _disposeClient; +#if !NET47 + // The callback most recently requested through Configuration.RemoteCertificateValidationCallback. + // The delegate installed on the HttpClientHandler reads this field, so it is refreshed on every request. + private volatile System.Net.Security.RemoteCertificateValidationCallback _remoteCertificateValidationCallback; + // 0 = not installed yet, 1 = installed (guarded with Interlocked for thread safety). + private int _remoteCertificateValidationInstalled; +#endif /// /// Specifies the settings on a object. @@ -255,7 +262,9 @@ public ApiClient(string basePath) /// /// /// Some configuration settings will not be applied without passing an HttpClientHandler. - /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. + /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings, Remote Certificate Validation Callback. + /// Handler-level settings (Proxy, Client Certificates, Remote Certificate Validation Callback) must be configured before the first request is sent, + /// because HttpClientHandler properties cannot be changed once it has started sending requests. /// public ApiClient(HttpClient client, HttpClientHandler handler = null) : this(client, Org.OpenAPITools.Client.GlobalConfiguration.Instance.BasePath, handler) @@ -272,7 +281,9 @@ public ApiClient(HttpClient client, HttpClientHandler handler = null) : /// /// /// Some configuration settings will not be applied without passing an HttpClientHandler. - /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. + /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings, Remote Certificate Validation Callback. + /// Handler-level settings (Proxy, Client Certificates, Remote Certificate Validation Callback) must be configured before the first request is sent, + /// because HttpClientHandler properties cannot be changed once it has started sending requests. /// public ApiClient(HttpClient client, string basePath, HttpClientHandler handler = null) { @@ -508,13 +519,39 @@ private async Task> ExecAsync(HttpRequestMessage req, if (configuration.RemoteCertificateValidationCallback != null) { if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor."); - // HttpClientHandler properties are frozen after the first request, so only assign when not already set. - if (_httpClientHandler.ServerCertificateCustomValidationCallback == null) +#if NET47 + // HttpClientHandler.ServerCertificateCustomValidationCallback is only available from .NET Framework 4.7.1. + throw new NotSupportedException("Configuration `RemoteCertificateValidationCallback` is not supported when targeting .NET Framework 4.7, because HttpClientHandler.ServerCertificateCustomValidationCallback requires .NET Framework 4.7.1 or later."); +#else + // Refresh the callback on every request; the delegate installed below reads this field, + // so a later request (or a replaced Configuration) is not stuck with the first callback. + _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback; + // Install the handler delegate only once. HttpClientHandler properties are frozen after the + // first request, so the callback must be configured before this client sends its first request + // (same limitation as `Proxy` and `ClientCertificates`). + if (Interlocked.CompareExchange(ref _remoteCertificateValidationInstalled, 1, 0) == 0 + && _httpClientHandler.ServerCertificateCustomValidationCallback == null) { - _httpClientHandler.ServerCertificateCustomValidationCallback = - (request, certificate, chain, sslPolicyErrors) => - configuration.RemoteCertificateValidationCallback(request, certificate, chain, sslPolicyErrors); + try + { + _httpClientHandler.ServerCertificateCustomValidationCallback = + (request, certificate, chain, sslPolicyErrors) => + { + var callback = _remoteCertificateValidationCallback; + // Fall back to the default policy if the callback was cleared after installation. + return callback != null + ? callback(request, certificate, chain, sslPolicyErrors) + : sslPolicyErrors == System.Net.Security.SslPolicyErrors.None; + }; + } + catch (InvalidOperationException ex) + { + // Allow the failure to be reported again instead of silently skipping validation later. + Interlocked.Exchange(ref _remoteCertificateValidationInstalled, 0); + throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` must be set before the first request is sent: the HttpClientHandler has already started sending requests and its properties can no longer be changed.", ex); + } } +#endif } diff --git a/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs index deca9fb5e07d..497c2b01405d 100644 --- a/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -202,6 +202,13 @@ public partial class ApiClient : IDisposable, ISynchronousClient, IAsynchronousC private readonly HttpClientHandler _httpClientHandler; private readonly HttpClient _httpClient; private readonly bool _disposeClient; +#if !NET47 + // The callback most recently requested through Configuration.RemoteCertificateValidationCallback. + // The delegate installed on the HttpClientHandler reads this field, so it is refreshed on every request. + private volatile System.Net.Security.RemoteCertificateValidationCallback _remoteCertificateValidationCallback; + // 0 = not installed yet, 1 = installed (guarded with Interlocked for thread safety). + private int _remoteCertificateValidationInstalled; +#endif /// /// Specifies the settings on a object. @@ -255,7 +262,9 @@ public ApiClient(string basePath) /// /// /// Some configuration settings will not be applied without passing an HttpClientHandler. - /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. + /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings, Remote Certificate Validation Callback. + /// Handler-level settings (Proxy, Client Certificates, Remote Certificate Validation Callback) must be configured before the first request is sent, + /// because HttpClientHandler properties cannot be changed once it has started sending requests. /// public ApiClient(HttpClient client, HttpClientHandler handler = null) : this(client, Org.OpenAPITools.Client.GlobalConfiguration.Instance.BasePath, handler) @@ -272,7 +281,9 @@ public ApiClient(HttpClient client, HttpClientHandler handler = null) : /// /// /// Some configuration settings will not be applied without passing an HttpClientHandler. - /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. + /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings, Remote Certificate Validation Callback. + /// Handler-level settings (Proxy, Client Certificates, Remote Certificate Validation Callback) must be configured before the first request is sent, + /// because HttpClientHandler properties cannot be changed once it has started sending requests. /// public ApiClient(HttpClient client, string basePath, HttpClientHandler handler = null) { @@ -508,13 +519,39 @@ private async Task> ExecAsync(HttpRequestMessage req, if (configuration.RemoteCertificateValidationCallback != null) { if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor."); - // HttpClientHandler properties are frozen after the first request, so only assign when not already set. - if (_httpClientHandler.ServerCertificateCustomValidationCallback == null) +#if NET47 + // HttpClientHandler.ServerCertificateCustomValidationCallback is only available from .NET Framework 4.7.1. + throw new NotSupportedException("Configuration `RemoteCertificateValidationCallback` is not supported when targeting .NET Framework 4.7, because HttpClientHandler.ServerCertificateCustomValidationCallback requires .NET Framework 4.7.1 or later."); +#else + // Refresh the callback on every request; the delegate installed below reads this field, + // so a later request (or a replaced Configuration) is not stuck with the first callback. + _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback; + // Install the handler delegate only once. HttpClientHandler properties are frozen after the + // first request, so the callback must be configured before this client sends its first request + // (same limitation as `Proxy` and `ClientCertificates`). + if (Interlocked.CompareExchange(ref _remoteCertificateValidationInstalled, 1, 0) == 0 + && _httpClientHandler.ServerCertificateCustomValidationCallback == null) { - _httpClientHandler.ServerCertificateCustomValidationCallback = - (request, certificate, chain, sslPolicyErrors) => - configuration.RemoteCertificateValidationCallback(request, certificate, chain, sslPolicyErrors); + try + { + _httpClientHandler.ServerCertificateCustomValidationCallback = + (request, certificate, chain, sslPolicyErrors) => + { + var callback = _remoteCertificateValidationCallback; + // Fall back to the default policy if the callback was cleared after installation. + return callback != null + ? callback(request, certificate, chain, sslPolicyErrors) + : sslPolicyErrors == System.Net.Security.SslPolicyErrors.None; + }; + } + catch (InvalidOperationException ex) + { + // Allow the failure to be reported again instead of silently skipping validation later. + Interlocked.Exchange(ref _remoteCertificateValidationInstalled, 0); + throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` must be set before the first request is sent: the HttpClientHandler has already started sending requests and its properties can no longer be changed.", ex); + } } +#endif } diff --git a/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs index 58a71fd45fc3..e9dbec351962 100644 --- a/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -200,6 +200,13 @@ public partial class ApiClient : IDisposable, ISynchronousClient, IAsynchronousC private readonly HttpClientHandler _httpClientHandler; private readonly HttpClient _httpClient; private readonly bool _disposeClient; +#if !NET47 + // The callback most recently requested through Configuration.RemoteCertificateValidationCallback. + // The delegate installed on the HttpClientHandler reads this field, so it is refreshed on every request. + private volatile System.Net.Security.RemoteCertificateValidationCallback _remoteCertificateValidationCallback; + // 0 = not installed yet, 1 = installed (guarded with Interlocked for thread safety). + private int _remoteCertificateValidationInstalled; +#endif /// /// Specifies the settings on a object. @@ -253,7 +260,9 @@ public ApiClient(string basePath) /// /// /// Some configuration settings will not be applied without passing an HttpClientHandler. - /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. + /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings, Remote Certificate Validation Callback. + /// Handler-level settings (Proxy, Client Certificates, Remote Certificate Validation Callback) must be configured before the first request is sent, + /// because HttpClientHandler properties cannot be changed once it has started sending requests. /// public ApiClient(HttpClient client, HttpClientHandler handler = null) : this(client, Org.OpenAPITools.Client.GlobalConfiguration.Instance.BasePath, handler) @@ -270,7 +279,9 @@ public ApiClient(HttpClient client, HttpClientHandler handler = null) : /// /// /// Some configuration settings will not be applied without passing an HttpClientHandler. - /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings. + /// The features affected are: Setting and Retrieving Cookies, Client Certificates, Proxy settings, Remote Certificate Validation Callback. + /// Handler-level settings (Proxy, Client Certificates, Remote Certificate Validation Callback) must be configured before the first request is sent, + /// because HttpClientHandler properties cannot be changed once it has started sending requests. /// public ApiClient(HttpClient client, string basePath, HttpClientHandler handler = null) { @@ -506,13 +517,39 @@ private async Task> ExecAsync(HttpRequestMessage req, if (configuration.RemoteCertificateValidationCallback != null) { if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor."); - // HttpClientHandler properties are frozen after the first request, so only assign when not already set. - if (_httpClientHandler.ServerCertificateCustomValidationCallback == null) +#if NET47 + // HttpClientHandler.ServerCertificateCustomValidationCallback is only available from .NET Framework 4.7.1. + throw new NotSupportedException("Configuration `RemoteCertificateValidationCallback` is not supported when targeting .NET Framework 4.7, because HttpClientHandler.ServerCertificateCustomValidationCallback requires .NET Framework 4.7.1 or later."); +#else + // Refresh the callback on every request; the delegate installed below reads this field, + // so a later request (or a replaced Configuration) is not stuck with the first callback. + _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback; + // Install the handler delegate only once. HttpClientHandler properties are frozen after the + // first request, so the callback must be configured before this client sends its first request + // (same limitation as `Proxy` and `ClientCertificates`). + if (Interlocked.CompareExchange(ref _remoteCertificateValidationInstalled, 1, 0) == 0 + && _httpClientHandler.ServerCertificateCustomValidationCallback == null) { - _httpClientHandler.ServerCertificateCustomValidationCallback = - (request, certificate, chain, sslPolicyErrors) => - configuration.RemoteCertificateValidationCallback(request, certificate, chain, sslPolicyErrors); + try + { + _httpClientHandler.ServerCertificateCustomValidationCallback = + (request, certificate, chain, sslPolicyErrors) => + { + var callback = _remoteCertificateValidationCallback; + // Fall back to the default policy if the callback was cleared after installation. + return callback != null + ? callback(request, certificate, chain, sslPolicyErrors) + : sslPolicyErrors == System.Net.Security.SslPolicyErrors.None; + }; + } + catch (InvalidOperationException ex) + { + // Allow the failure to be reported again instead of silently skipping validation later. + Interlocked.Exchange(ref _remoteCertificateValidationInstalled, 0); + throw new InvalidOperationException("Configuration `RemoteCertificateValidationCallback` must be set before the first request is sent: the HttpClientHandler has already started sending requests and its properties can no longer be changed.", ex); + } } +#endif } var cookieContainer = req.Properties.ContainsKey("CookieContainer") ? req.Properties["CookieContainer"] as List : null; From e7a6bf2a2a35473c17917c687aad12bda5894ed5 Mon Sep 17 00:00:00 2001 From: "Nagabalaji Siddaiah Buddala -X (nsiddaia - XORIANT SOLUTIONS PRIVATE LIMITED at Cisco)" Date: Thu, 26 Nov 2026 20:20:27 +0530 Subject: [PATCH 4/4] [C#][httpclient] Honor Configuration.RemoteCertificateValidationCallback The httpclient ApiClient accepted Configuration.RemoteCertificateValidationCallback but never applied it, so the setting was silently ignored. The callback is now assigned to the HttpClientHandler, with the following behavior: - Stored in a field refreshed on each request. If a later request has no callback, the stored one is cleared, so default validation applies. - Installed once; a callback set on a consumer-provided handler is not overwritten. - Throws a clear InvalidOperationException if the handler is already frozen. - Not supported on net47 (ServerCertificateCustomValidationCallback is unavailable there). The handler is shared by all requests of a client, so the most recent request's configuration wins. No change when the callback is not set. Regenerated httpclient samples. --- .../csharp/libraries/httpclient/ApiClient.mustache | 11 +++++++++++ .../src/Org.OpenAPITools/Client/ApiClient.cs | 11 +++++++++++ .../Petstore/src/Org.OpenAPITools/Client/ApiClient.cs | 11 +++++++++++ .../src/Org.OpenAPITools/Client/ApiClient.cs | 11 +++++++++++ .../Petstore/src/Org.OpenAPITools/Client/ApiClient.cs | 11 +++++++++++ .../Petstore/src/Org.OpenAPITools/Client/ApiClient.cs | 11 +++++++++++ 6 files changed, 66 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache index 5bd5141d8130..598c496bb45c 100644 --- a/modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/libraries/httpclient/ApiClient.mustache @@ -530,6 +530,8 @@ namespace {{packageName}}.Client #else // Refresh the callback on every request; the delegate installed below reads this field, // so a later request (or a replaced Configuration) is not stuck with the first callback. + // The HttpClientHandler is shared by all requests of this client, so the callback applies + // client-wide and the most recent request's configuration wins. _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback; // Install the handler delegate only once. HttpClientHandler properties are frozen after the // first request, so the callback must be configured before this client sends its first request @@ -558,6 +560,15 @@ namespace {{packageName}}.Client } #endif } +#if !NET47 + else + { + // No callback configured for this request: clear any callback stored by an earlier request, + // so an already installed delegate falls back to the default validation instead of reusing it. + // This is a no-op for clients that never set Configuration.RemoteCertificateValidationCallback. + _remoteCertificateValidationCallback = null; + } +#endif {{^net60OrLater}} var cookieContainer = req.Properties.ContainsKey("CookieContainer") ? req.Properties["CookieContainer"] as List : null; diff --git a/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs index 142a13358d89..32147b2f5f21 100644 --- a/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs @@ -525,6 +525,8 @@ private async Task> ExecAsync(HttpRequestMessage req, #else // Refresh the callback on every request; the delegate installed below reads this field, // so a later request (or a replaced Configuration) is not stuck with the first callback. + // The HttpClientHandler is shared by all requests of this client, so the callback applies + // client-wide and the most recent request's configuration wins. _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback; // Install the handler delegate only once. HttpClientHandler properties are frozen after the // first request, so the callback must be configured before this client sends its first request @@ -553,6 +555,15 @@ private async Task> ExecAsync(HttpRequestMessage req, } #endif } +#if !NET47 + else + { + // No callback configured for this request: clear any callback stored by an earlier request, + // so an already installed delegate falls back to the default validation instead of reusing it. + // This is a no-op for clients that never set Configuration.RemoteCertificateValidationCallback. + _remoteCertificateValidationCallback = null; + } +#endif if (req.Options.TryGetValue(_httpOptionsCookieContainerKey, out var cookieContainer)) diff --git a/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs index 497c2b01405d..a9cf73b4f234 100644 --- a/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -525,6 +525,8 @@ private async Task> ExecAsync(HttpRequestMessage req, #else // Refresh the callback on every request; the delegate installed below reads this field, // so a later request (or a replaced Configuration) is not stuck with the first callback. + // The HttpClientHandler is shared by all requests of this client, so the callback applies + // client-wide and the most recent request's configuration wins. _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback; // Install the handler delegate only once. HttpClientHandler properties are frozen after the // first request, so the callback must be configured before this client sends its first request @@ -553,6 +555,15 @@ private async Task> ExecAsync(HttpRequestMessage req, } #endif } +#if !NET47 + else + { + // No callback configured for this request: clear any callback stored by an earlier request, + // so an already installed delegate falls back to the default validation instead of reusing it. + // This is a no-op for clients that never set Configuration.RemoteCertificateValidationCallback. + _remoteCertificateValidationCallback = null; + } +#endif if (req.Options.TryGetValue(_httpOptionsCookieContainerKey, out var cookieContainer)) diff --git a/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs index 142a13358d89..32147b2f5f21 100644 --- a/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs @@ -525,6 +525,8 @@ private async Task> ExecAsync(HttpRequestMessage req, #else // Refresh the callback on every request; the delegate installed below reads this field, // so a later request (or a replaced Configuration) is not stuck with the first callback. + // The HttpClientHandler is shared by all requests of this client, so the callback applies + // client-wide and the most recent request's configuration wins. _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback; // Install the handler delegate only once. HttpClientHandler properties are frozen after the // first request, so the callback must be configured before this client sends its first request @@ -553,6 +555,15 @@ private async Task> ExecAsync(HttpRequestMessage req, } #endif } +#if !NET47 + else + { + // No callback configured for this request: clear any callback stored by an earlier request, + // so an already installed delegate falls back to the default validation instead of reusing it. + // This is a no-op for clients that never set Configuration.RemoteCertificateValidationCallback. + _remoteCertificateValidationCallback = null; + } +#endif if (req.Options.TryGetValue(_httpOptionsCookieContainerKey, out var cookieContainer)) diff --git a/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs index 497c2b01405d..a9cf73b4f234 100644 --- a/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -525,6 +525,8 @@ private async Task> ExecAsync(HttpRequestMessage req, #else // Refresh the callback on every request; the delegate installed below reads this field, // so a later request (or a replaced Configuration) is not stuck with the first callback. + // The HttpClientHandler is shared by all requests of this client, so the callback applies + // client-wide and the most recent request's configuration wins. _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback; // Install the handler delegate only once. HttpClientHandler properties are frozen after the // first request, so the callback must be configured before this client sends its first request @@ -553,6 +555,15 @@ private async Task> ExecAsync(HttpRequestMessage req, } #endif } +#if !NET47 + else + { + // No callback configured for this request: clear any callback stored by an earlier request, + // so an already installed delegate falls back to the default validation instead of reusing it. + // This is a no-op for clients that never set Configuration.RemoteCertificateValidationCallback. + _remoteCertificateValidationCallback = null; + } +#endif if (req.Options.TryGetValue(_httpOptionsCookieContainerKey, out var cookieContainer)) diff --git a/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs index e9dbec351962..0fb75e232b7e 100644 --- a/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -523,6 +523,8 @@ private async Task> ExecAsync(HttpRequestMessage req, #else // Refresh the callback on every request; the delegate installed below reads this field, // so a later request (or a replaced Configuration) is not stuck with the first callback. + // The HttpClientHandler is shared by all requests of this client, so the callback applies + // client-wide and the most recent request's configuration wins. _remoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback; // Install the handler delegate only once. HttpClientHandler properties are frozen after the // first request, so the callback must be configured before this client sends its first request @@ -551,6 +553,15 @@ private async Task> ExecAsync(HttpRequestMessage req, } #endif } +#if !NET47 + else + { + // No callback configured for this request: clear any callback stored by an earlier request, + // so an already installed delegate falls back to the default validation instead of reusing it. + // This is a no-op for clients that never set Configuration.RemoteCertificateValidationCallback. + _remoteCertificateValidationCallback = null; + } +#endif var cookieContainer = req.Properties.ContainsKey("CookieContainer") ? req.Properties["CookieContainer"] as List : null;