【问题标题】:DotNet Core HttpClient security errorDotNet Core HttpClient 安全错误
【发布时间】:2018-06-25 06:36:15
【问题描述】:

我有一个类库来执行 4.6.1 框架中内置的 Rest API 调用。我使用 System.Net.Http V4 HttpClient 来管理调用。该库适用于普通的 dotnet 应用程序。最近我尝试了一个 DotNet Core 应用程序,它因安全错误而失败。后来我按照post 中的建议修改了库。它取得了一些进展,但应用程序失败并出现错误,如下所示

错误消息:发送请求时出错。 内部异常消息:发生安全错误。 堆栈跟踪: 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Threading.Tasks.RendezvousAwaitable`1.GetResult() 在 System.Net.Http.WinHttpHandler.d__105.MoveNext()

库代码:

private readonly HttpClient _client = null;
private ICredentials somecred;

    public Program()
    {
        HttpClientHandler clientHandler = new HttpClientHandler { Credentials = somecred, UseDefaultCredentials = false };
        _client=new HttpClient(new MessageHandler(clientHandler, TimeSpan.FromSeconds(1), 1), false);
        _client.Timeout = TimeSpan.FromSeconds(90);
        _client.BaseAddress = new Uri("https://somedomain.com/");

        ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
        ServicePointManager.DefaultConnectionLimit = 10;
        ServicePointManager.Expect100Continue = true;
    }

    public async Task<IOperationResponse> GetData()
    {
        ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

        using (HttpResponseMessage httpResponse =
                await _client.SendAsync(new HttpRequestMessage(HttpMethod.Head, "api/getdata"), HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false)
                )
        {
            if (httpResponse != null) { ... }
        }
    }

    internal class MessageHandler : DelegatingHandler
    {
        public MessageHandler(HttpMessageHandler innerHandler, TimeSpan retryInterval, int retryCount) : base(innerHandler)
        {

        }

        protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request,
        CancellationToken cancellationToken)
        {
            HttpResponseMessage response = null;

            //!! security error in below line
            response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
        }
    }

请告诉我,该库需要做什么才能运行 DotNet Core 应用程序。谢谢。

【问题讨论】:

    标签: .net-core httpclient


    【解决方案1】:

    我已经使用核心项目和外部 4.5 库在本地测试了您的代码,我可以触发安全错误的唯一方法是目标站点没有正确的安全级别(即没有正确设置SSL)。当定位一个正确配置了 SSL 的站点时,调用成功了。

    确保您的目标已设置 SSL 或以另一个已知工作 SSL 站点为目标进行测试,以查看您是否仍然遇到问题。

    【讨论】:

    • 你可能是对的。目标是供应商,我没有太多控制权。有什么方法可以测试目标是否从我这边设置了 SSL?
    • 在浏览器中使用 https:// 转到有问题的 URL,看看它是否确实提供了证书。还有一些在线工具可以扫描网站(假设它不是私人网址)。
    【解决方案2】:

    我根据同事的建议做了以下代码更改并修复了它。

    //original code
    _client.BaseAddress = new Uri("https://xyz.abc.somedomain.com/");
    
    //fix code
    _client.BaseAddress = new Uri("https://abc.somedomain.com/");
    _client.DefaultRequestHeaders.Host = "xyz.abc.somedomain.com";
    

    我将此标记为答案,尽管我并不完全理解这种行为。

    【讨论】:

      猜你喜欢
      • 2018-08-27
      • 2020-03-22
      • 1970-01-01
      • 2017-10-27
      • 2017-08-21
      • 2018-07-21
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      相关资源
      最近更新 更多