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..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 @@ -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) { @@ -510,6 +521,55 @@ 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."); +#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. + // 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 + // (same limitation as `Proxy` and `ClientCertificates`). + if (Interlocked.CompareExchange(ref _remoteCertificateValidationInstalled, 1, 0) == 0 + && _httpClientHandler.ServerCertificateCustomValidationCallback == null) + { + 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 + } +#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; {{/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 01ed136022b0..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 @@ -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) { @@ -505,6 +516,55 @@ 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."); +#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. + // 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 + // (same limitation as `Proxy` and `ClientCertificates`). + if (Interlocked.CompareExchange(ref _remoteCertificateValidationInstalled, 1, 0) == 0 + && _httpClientHandler.ServerCertificateCustomValidationCallback == null) + { + 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 + } +#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 1e3edc1fec0f..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 @@ -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) { @@ -505,6 +516,55 @@ 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."); +#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. + // 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 + // (same limitation as `Proxy` and `ClientCertificates`). + if (Interlocked.CompareExchange(ref _remoteCertificateValidationInstalled, 1, 0) == 0 + && _httpClientHandler.ServerCertificateCustomValidationCallback == null) + { + 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 + } +#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 01ed136022b0..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 @@ -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) { @@ -505,6 +516,55 @@ 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."); +#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. + // 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 + // (same limitation as `Proxy` and `ClientCertificates`). + if (Interlocked.CompareExchange(ref _remoteCertificateValidationInstalled, 1, 0) == 0 + && _httpClientHandler.ServerCertificateCustomValidationCallback == null) + { + 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 + } +#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 1e3edc1fec0f..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 @@ -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) { @@ -505,6 +516,55 @@ 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."); +#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. + // 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 + // (same limitation as `Proxy` and `ClientCertificates`). + if (Interlocked.CompareExchange(ref _remoteCertificateValidationInstalled, 1, 0) == 0 + && _httpClientHandler.ServerCertificateCustomValidationCallback == null) + { + 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 + } +#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 f6913dca419e..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 @@ -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) { @@ -503,6 +514,55 @@ 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."); +#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. + // 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 + // (same limitation as `Proxy` and `ClientCertificates`). + if (Interlocked.CompareExchange(ref _remoteCertificateValidationInstalled, 1, 0) == 0 + && _httpClientHandler.ServerCertificateCustomValidationCallback == null) + { + 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 + } +#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; if (cookieContainer != null)