Flurl和不受信任的证书

Dig*_*tag 4 .net c# certificate flurl

目前,我从事Flurl的研究,并尝试通过https与API联系(我在实验室中)。因此该证书无效,并且Flurl无法继续工作:/

这是我的错误信息:

Unhandled Exception: System.AggregateException: One or more errors occurred. (Call failed. The SSL connection could not be established, see inner exception. POST https://IP/api/aaaLogin.json) ---> Flurl.Http.FlurlHttpException: Call failed. The SSL connection could not be established, see inner exception. POST https://IP/api/aaaLogin.json ---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
Run Code Online (Sandbox Code Playgroud)

在Flurl文档中,我们可以使用using Flurl.Http.Configuration;和修改。DefaultHttpClientFactory但是,我不理解所指定要跳过的元素。

在网络上,我可以看到相同的情况:https : //github.com/tmenier/Flurl/issues/365 您是否对此问题有疑问?

谢谢!

Tod*_*ier 8

最典型的方法是创建自定义工厂

public class UntrustedCertClientFactory : DefaultHttpClientFactory
{
    public override HttpMessageHandler CreateMessageHandler() {
        return new HttpClientHandler {
            ServerCertificateCustomValidationCallback = (a, b, c, d) => true
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在您的应用启动时将其注册:

FlurlHttp.ConfigureClient("https://theapi.com", cli =>
    cli.Settings.HttpClientFactory = new UntrustedCertClientFactory());
Run Code Online (Sandbox Code Playgroud)

Flurl HttpClient默认情况下会在每个主机上重用同一实例,因此配置此方式意味着每次调用都theapi.com将允许使用不受信任的证书。与将an传递HttpClientFlurlClient构造函数相比,此方法的优点在于,它使此配置“脱离侧边”,并且在以更典型/更冗长的方式使用Flurl时可以使用:

await "https://theapi.com/endpoint".GetJsonAsync();
Run Code Online (Sandbox Code Playgroud)

  • 嗨@ToddMenier 感谢您的更新。我只需要更改语法。`public class UntrustedCertClientFactory : DefaultHttpClientFactory { public override HttpMessageHandler CreateMessageHandler() => new HttpClientHandler { ServerCertificateCustomValidationCallback = (a, b, c, d) => true }; }` (2认同)

Kar*_*ral 6

这是我对 Flurl 的设置,它适用于不受信任的证书:

HttpClientHandler httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, 
  errors) => true;
HttpClient httpClient = new HttpClient(httpClientHandler);
httpClient.BaseAddress = new Uri("https://myaddress.com");
var flurlClient = new FlurlClient(httpClient);

var apiInfo = await flurlClient.Request("apiInfo").GetJsonAsync<ApiInfoDto>();
Run Code Online (Sandbox Code Playgroud)

我创建了自定义 HttpClientHandler ,它接受ServerCertificateCustomValidationCallback. 当然,您可以在此处理程序中使用其他逻辑。

更新: 使用此设置,您不能对 URL 使用 Flurl 扩展(您不能编写"http://myadress.com/apiInfo".GetJsonAsync<ApiInfoDto>(). 您必须创建如上所示的 Flurl 客户端,并使用 Flurl 客户端进行调用,如我的代码中所示。用法与 Flurl 扩展相同网址。


luf*_*ist 5

接受任何证书的内联解决方案是:


var myString = await "https://some-server-with-an-invalid-cert.net"
    .AppendPathSegment("/some-file.txt")
    .WithClient(new FlurlClient(new HttpClient(new HttpClientHandler
              {
                  ServerCertificateCustomValidationCallback = (message, cert, chain,
                                                               errors) => true
              })))
    .GetStringAsync();
Run Code Online (Sandbox Code Playgroud)

WithClient()可以传递配置与默认客户端不同的客户端。在某些情况下,您不想更改默认客户端,而是应用属性,例如仅针对此特定情况应用证书验证。