c# - 在 HttpClient 中使用受信任的根证书颁发机构进行服务器证书验证

标签 c# ssl https asp.net-core .net-core

我已经使用

创建了一个自签名证书
openssl req -x509 -newkey rsa:2048 -keyout https-key.pem -out https.pem -days 365

然后我创建了 pkcs12 使用(我已经将 CN 设置为我服务器的 ip 地址):

openssl pkcs12 -export -out https.pfx -inkey https-key.pem -in https.pem -password pass:123456

在我的服务器中,为 https 使用生成的 https.pfx 文件。

在我的客户端中,我将生成的证书导入 Windows 的 Trusted Root Certification Authorities(当前用户和本地系统)。

当我从客户端向服务器发送 HTTP 请求时,我得到了

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: A security error occurred
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.Tasks.RendezvousAwaitable`1.GetResult()
   at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task).

在 chrome 中它说:

Attackers might be trying to steal your information from *** (for example, passwords, messages, or credit cards). NET::ERR_CERT_AUTHORITY_INVALID

.NET Core 2.0 的 HttpClient 是否使用 Windows 证书存储?可能导致问题的原因。

最佳答案

我建议您创建一个小型测试应用程序,您可以将其用作客户端来测试服务器的 TLS 配置。

您可以使用 SslStream class在 .NET 中连接到 TLS 端点并执行协商。与来自 SChannel 的随机 HRESULT 代码和无用的错误消息相比,它可以返回更易于理解的错误消息。

测试工具可以是包含以下代码的 WinForms 应用程序:

public static void q48873455()
{
    const string hostname = "localhost";
    var tcpClient = new TcpClient();
    tcpClient.Connect(hostname, 443);
    var tcpStream = tcpClient.GetStream();

    using (var sslStream = new SslStream(tcpStream, false, ValidateServerCertificate))
    {
        sslStream.AuthenticateAsClient(hostname);
    }
}


static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
   if (sslPolicyErrors == SslPolicyErrors.None)
        return true;

    Console.WriteLine("Certificate error: {0}", sslPolicyErrors);

    return false;
}

此代码建立了到端口 443 的原始 TCP 连接,然后使用 SslStream 执行 TLS 协商。在协商期间,服务器将发送其证书,SslStream 将调用 ValidateServerCertificate() 方法,传递它收到的 certificate,并且为 SslPolicyErrors 提供一个值,指示 SChannel 对证书的看法。

SslPolicyErrors 值是 MSDN 中列出的值之一这表明为什么(如果有的话)SChannel 认为证书不受信任。

我倾向于发现,在尝试找出给定证书不受信任的原因时,拥有诸如此类的工具非常有帮助。

希望对你有帮助

关于c# - 在 HttpClient 中使用受信任的根证书颁发机构进行服务器证书验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48873455/

相关文章:

c# - GetChanges() 或 RowState 获取数据表的所有更改?

如果禁用 SSL v3 和 TLS v1.0,Android 4.x 无法连接到 Worklight Server

在主机上安装 SSL 证书,生成 key

尝试连接到 mailtrap smtp 时,获得第一条记录看起来不像 TLS 握手

c# - 更好地处理丢失的 .NET Framework (0xc0000135) 崩溃?

c# - 我可以更改默认配置文件吗?

ssl - Tomcat5 SSL 不工作

apache - SSL认证apache

ssl - 什么是 SSL,它与 HTTPS 有什么关系?

c# - 如何使用任何脚本判断给定的 URL 是否会重定向?