Repository navigation
[C#][generichost] Support skipping certificate validation for HttpClient #25315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
7a016f2
d60e3ff
85a408b
cd3b33e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -83,6 +83,17 @@ namespace YourProject | |||||
| } | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| ## Certificate validation | ||||||
|
|
||||||
| To skip certificate validation for local development or testing, configure the generated HTTP clients as follows: | ||||||
|
|
||||||
| ```cs | ||||||
| options.AddApiHttpClients(client => { }, builder => builder.SkipCertificateCheck()); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: This example leaves Prompt for AI agents
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| Do not disable certificate validation in production. | ||||||
|
|
||||||
| <a id="questions"></a> | ||||||
| ## Questions | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| using System; | ||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Net.Http; | ||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Net.Security; | ||||||||||||||||||||||||||||||||||||||||||||||||
| using Microsoft.Extensions.Http; | ||||||||||||||||||||||||||||||||||||||||||||||||
| using Microsoft.Extensions.DependencyInjection; | ||||||||||||||||||||||||||||||||||||||||||||||||
| using Polly.Timeout; | ||||||||||||||||||||||||||||||||||||||||||||||||
| using Polly.Extensions.Http; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -23,6 +25,59 @@ namespace Org.OpenAPITools.Extensions | |||||||||||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||||||||||||
| public static class IHttpClientBuilderExtensions | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// Configures remote certificate validation for requests made by this client. | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||||||||||||
| public static IHttpClientBuilder ConfigureRemoteCertificateValidationCallback( | ||||||||||||||||||||||||||||||||||||||||||||||||
| this IHttpClientBuilder client, | ||||||||||||||||||||||||||||||||||||||||||||||||
| RemoteCertificateValidationCallback callback) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (callback == null) | ||||||||||||||||||||||||||||||||||||||||||||||||
| throw new ArgumentNullException(nameof(callback)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| client.Services.Configure<HttpClientFactoryOptions>(client.Name, (HttpClientFactoryOptions options) => | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| options.HttpMessageHandlerBuilderActions.Add(builder => | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (builder.PrimaryHandler is HttpClientHandler httpClientHandler) | ||||||||||||||||||||||||||||||||||||||||||||||||
| httpClientHandler.ServerCertificateCustomValidationCallback = (request, certificate, chain, errors) => | ||||||||||||||||||||||||||||||||||||||||||||||||
| callback(request, certificate, chain, errors); | ||||||||||||||||||||||||||||||||||||||||||||||||
| #if NETCOREAPP || NETSTANDARD2_1_OR_GREATER | ||||||||||||||||||||||||||||||||||||||||||||||||
| else if (builder.PrimaryHandler is SocketsHttpHandler socketsHttpHandler) | ||||||||||||||||||||||||||||||||||||||||||||||||
| socketsHttpHandler.SslOptions.RemoteCertificateValidationCallback = callback; | ||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||
| throw new InvalidOperationException("Certificate validation requires HttpClientHandler or SocketsHttpHandler."); | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return client; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// Disables TLS certificate validation for requests made by this client. | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// Use only for development or testing. | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||||||||||||
| public static IHttpClientBuilder SkipCertificateCheck(this IHttpClientBuilder client) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| client.Services.Configure<HttpClientFactoryOptions>(client.Name, (HttpClientFactoryOptions options) => | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| options.HttpMessageHandlerBuilderActions.Add(builder => | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (builder.PrimaryHandler is HttpClientHandler httpClientHandler) | ||||||||||||||||||||||||||||||||||||||||||||||||
| httpClientHandler.ServerCertificateCustomValidationCallback = (request, certificate, chain, errors) => true; | ||||||||||||||||||||||||||||||||||||||||||||||||
| #if NETCOREAPP || NETSTANDARD2_1_OR_GREATER | ||||||||||||||||||||||||||||||||||||||||||||||||
| else if (builder.PrimaryHandler is SocketsHttpHandler socketsHttpHandler) | ||||||||||||||||||||||||||||||||||||||||||||||||
| socketsHttpHandler.SslOptions.RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true; | ||||||||||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||
| throw new InvalidOperationException("Certificate validation requires HttpClientHandler or SocketsHttpHandler."); | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| return client; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+61
to
+79
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: Prompt for AI agents
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// Adds a Polly retry policy to your clients. | ||||||||||||||||||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -80,6 +80,17 @@ namespace YourProject | |||||
| } | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| ## Certificate validation | ||||||
|
|
||||||
| To skip certificate validation for local development or testing, configure the generated HTTP clients as follows: | ||||||
|
|
||||||
| ```cs | ||||||
| options.AddApiHttpClients(client => { }, builder => builder.SkipCertificateCheck()); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: The empty Prompt for AI agents
Suggested change
|
||||||
| ``` | ||||||
|
|
||||||
| Do not disable certificate validation in production. | ||||||
|
|
||||||
| <a id="questions"></a> | ||||||
| ## Questions | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,8 @@ | |
|
|
||
| using System; | ||
| using System.Net.Http; | ||
| using System.Net.Security; | ||
| using Microsoft.Extensions.Http; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Polly.Timeout; | ||
| using Polly.Extensions.Http; | ||
|
|
@@ -23,6 +25,59 @@ namespace Org.OpenAPITools.Extensions | |
| /// </summary> | ||
| public static class IHttpClientBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Configures remote certificate validation for requests made by this client. | ||
| /// </summary> | ||
| public static IHttpClientBuilder ConfigureRemoteCertificateValidationCallback( | ||
| this IHttpClientBuilder client, | ||
| RemoteCertificateValidationCallback callback) | ||
| { | ||
| if (callback == null) | ||
| throw new ArgumentNullException(nameof(callback)); | ||
|
|
||
| client.Services.Configure<HttpClientFactoryOptions>(client.Name, (HttpClientFactoryOptions options) => | ||
| { | ||
| options.HttpMessageHandlerBuilderActions.Add(builder => | ||
| { | ||
| if (builder.PrimaryHandler is HttpClientHandler httpClientHandler) | ||
| httpClientHandler.ServerCertificateCustomValidationCallback = (request, certificate, chain, errors) => | ||
| callback(request, certificate, chain, errors); | ||
| #if NETCOREAPP || NETSTANDARD2_1_OR_GREATER | ||
| else if (builder.PrimaryHandler is SocketsHttpHandler socketsHttpHandler) | ||
| socketsHttpHandler.SslOptions.RemoteCertificateValidationCallback = callback; | ||
| #endif | ||
| else | ||
| throw new InvalidOperationException("Certificate validation requires HttpClientHandler or SocketsHttpHandler."); | ||
| }); | ||
| }); | ||
|
|
||
| return client; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Disables TLS certificate validation for requests made by this client. | ||
| /// Use only for development or testing. | ||
| /// </summary> | ||
| public static IHttpClientBuilder SkipCertificateCheck(this IHttpClientBuilder client) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: Prompt for AI agents |
||
| { | ||
| client.Services.Configure<HttpClientFactoryOptions>(client.Name, (HttpClientFactoryOptions options) => | ||
| { | ||
| options.HttpMessageHandlerBuilderActions.Add(builder => | ||
| { | ||
| if (builder.PrimaryHandler is HttpClientHandler httpClientHandler) | ||
| httpClientHandler.ServerCertificateCustomValidationCallback = (request, certificate, chain, errors) => true; | ||
| #if NETCOREAPP || NETSTANDARD2_1_OR_GREATER | ||
| else if (builder.PrimaryHandler is SocketsHttpHandler socketsHttpHandler) | ||
| socketsHttpHandler.SslOptions.RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true; | ||
| #endif | ||
| else | ||
| throw new InvalidOperationException("Certificate validation requires HttpClientHandler or SocketsHttpHandler."); | ||
| }); | ||
| }); | ||
|
|
||
| return client; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a Polly retry policy to your clients. | ||
| /// </summary> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3:
SkipCertificateCheckduplicates the full handler-configuration block fromConfigureRemoteCertificateValidationCallback. Delegate to it with an always-true callback so the handler-type logic lives in one place.Prompt for AI agents