允许与HttpClient的不受信任的SSL证书(Allowing Untrusted SSL Ce

2019-06-18 06:39发布

我努力让我的Windows 8应用程序与我的测试网络API通过SSL进行通信。

看来,HttpClient的/ HttpClientHandler不提供和选择忽略不受信任的证书一样的WebRequest,您可以(虽然与“哈克”的方式ServerCertificateValidationCallback )。

任何帮助将非常感激!

Answer 1:

在Windows 8.1,您现在可以信任无效的SSL证书。 你必须要么使用Windows.Web.HttpClient或者如果你想使用System.Net.Http.HttpClient,您可以使用消息处理程序适配器我写道: http://www.nuget.org/packages/WinRtHttpClientHandler

文档都在GitHub上: https://github.com/onovotny/WinRtHttpClientHandler



Answer 2:

一个快速和肮脏的解决方案是使用ServicePointManager.ServerCertificateValidationCallback委托。 这使您可以提供自己的证书验证。 验证是在整个应用程序域全局上。

ServicePointManager.ServerCertificateValidationCallback +=
    (sender, cert, chain, sslPolicyErrors) => true;

我用这主要是为了在我想对我在过程中我托管的端点上运行的情况下进行单元测试,我试图用打它WCF客户端或HttpClient

对于生产代码,您可能希望更精细的控制,并会过得更好使用WebRequestHandler及其ServerCertificateValidationCallback委托财产(见下文DTB的答案 )。 或ctacke 回答使用HttpClientHandler 。 我甚至跟我的集成测试过我怎么用来做什么的,除非我无法找到任何其他钩现在宁愿这两种的。



Answer 3:

看一看在WebRequestHandler类及其ServerCertificateValidationCallback属性 :

using (var handler = new WebRequestHandler())
{
    handler.ServerCertificateValidationCallback = ...

    using (var client = new HttpClient(handler))
    {
        ...
    }
}


Answer 4:

如果你试图做这在.NET标准库,这里有一个简单的解决方案,所有的只是返回的风险true在你的处理器。 我离开安全由你。

var handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ServerCertificateCustomValidationCallback = 
    (httpRequestMessage, cert, cetChain, policyErrors) =>
{
    return true;
};

var client = new HttpClient(handler);


Answer 5:

或者您可以使用在HttpClientWindows.Web.Http命名空间:

var filter = new HttpBaseProtocolFilter();
#if DEBUG
    filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired);
    filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
    filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);
#endif
using (var httpClient = new HttpClient(filter)) {
    ...
}


Answer 6:

如果这是一个Windows运行时应用程序,则必须将自签名证书添加到项目,并在appxmanifest引用它。

该文档是在这里: http://msdn.microsoft.com/en-us/library/windows/apps/hh465031.aspx

你需要得到CA的公钥证书,它作为内容添加到应用程序,然后将其添加到清单 - 如果它是来自那不是值得信赖的(如私有CA机器本身不信任)一个CA同样的事情。

一旦这样做了,应用程序将认为这是一个正确签名的证书。



Answer 7:

大多数的答案在这里建议使用典型模式:

using (var httpClient = new HttpClient())
{
 // do something
}

因为IDisposable接口的。 请不要!

微软告诉你为什么:

  • https://docs.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation
  • https://blogs.msdn.microsoft.com/henrikn/2012/08/07/httpclient-httpclienthandler-and-webrequesthandler-explained/

在这里,你可以找到怎么回事幕后的详细分析: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

关于你提到的SSL问题,并根据https://docs.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/#how-to-fix-the-problem

这里是你的模式:

class HttpInterface
{
 // https://docs.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/#how-to-fix-the-problem
 // https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient#remarks
 private static readonly HttpClient client;

 // static initialize
 static HttpInterface()
 {
  // choose one of these depending on your framework

  // HttpClientHandler is an HttpMessageHandler with a common set of properties
  var handler = new HttpClientHandler();
  {
      ServerCertificateCustomValidationCallback = delegate { return true; },
  };
  // derives from HttpClientHandler but adds properties that generally only are available on full .NET
  var handler = new WebRequestHandler()
  {
      ServerCertificateValidationCallback = delegate { return true; },
      ServerCertificateCustomValidationCallback = delegate { return true; },
  };

  client = new HttpClient(handler);
 }

 .....

 // in your code use the static client to do your stuff
 var jsonEncoded = new StringContent(someJsonString, Encoding.UTF8, "application/json");

 // here in sync
 using (HttpResponseMessage resultMsg = client.PostAsync(someRequestUrl, jsonEncoded).Result)
 {
  using (HttpContent respContent = resultMsg.Content)
  {
   return respContent.ReadAsStringAsync().Result;
  }
 }
}


Answer 8:

我不知道答案,但我有一个替代。

如果您使用Fiddler2监控流量和启用HTTPS解密,你的开发环境也不会抱怨。 这会不会对WinRT的设备,如微软Surface的工作,因为你不能在其上安装的应用程序的标准。 但是,你的开发Win8的计算机将被罚款。

要启用Fiddler2 HTTPS加密,转到工具>选项提琴手> HTTPS(标签)>检查“解密HTTPS流量”。

我要保持我的眼睛在此线程希望有人有一个优雅的解决方案。



Answer 9:

我发现了一个例子在线,这似乎很好地工作:

首先创建一个新的ICertificatePolicy

using System.Security.Cryptography.X509Certificates;
using System.Net;

public class MyPolicy : ICertificatePolicy
{
  public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, 
int certificateProblem)
  {
    //Return True to force the certificate to be accepted.
    return true;
  }
}

然后,只需使用你发送HTTP请求,像这样在此之前:

System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();

http://www.terminally-incoherent.com/blog/2008/05/05/send-a-https-post-request-with-c/



文章来源: Allowing Untrusted SSL Certificates with HttpClient