Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在HttpClient中使用受信任的根证书颁发机构进行服务器证书验证_C#_Ssl_Https_Asp.net Core_.net Core - Fatal编程技术网

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

C# 在HttpClient中使用受信任的根证书颁发机构进行服务器证书验证,c#,ssl,https,asp.net-core,.net-core,C#,Ssl,Https,Asp.net Core,.net Core,我已使用创建了自签名证书 openssl req -x509 -newkey rsa:2048 -keyout https-key.pem -out https.pem -days 365 然后,我使用(我已将CN设置为服务器的ip地址)创建了pkcs12: 在我的服务器a中,将生成的https.pfx文件用于https 在我的客户端中,我将生成的证书导入Windows的受信任根证书颁发机构(当前用户和本地系统) 当我从客户端向服务器发送HTTP请求时,我得到 System.Net.Http.

我已使用创建了自签名证书

openssl req -x509 -newkey rsa:2048 -keyout https-key.pem -out https.pem -days 365
然后,我使用(我已将CN设置为服务器的ip地址)创建了pkcs12:

在我的服务器a中,将生成的
https.pfx
文件用于https

在我的客户端中,我将生成的证书导入Windows的受信任根证书颁发机构(当前用户和本地系统)

当我从客户端向服务器发送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).

.NET Core 2.0的HttpClient是否使用Windows证书存储?问题可能是由什么引起的。

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

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

测试工具只能是包含以下代码的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()
方法,传递它接收到的
证书,并为
SslPolicyErrors
提供一个值,该值指示SChannel对证书的看法

SslPolicyErrors
值是上面列出的值之一,它表明了SChannel认为证书不可信的原因(如果有的话)

我倾向于发现,当试图找出给定证书不可信的原因时,有这样一个工具非常有用


希望这有帮助

我认为您遇到了windows凭据问题。要使两台PC在windows中通信,它们必须在同一组中。如果您在公司网络中,则适用集团政策,您需要向您的MIS人员咨询,以便将两台电脑放在同一集团中。网站通常具有来宾凭据,因此不需要群组。Trusted Root表示两台电脑在同一组中,因此不需要密码,因为正在使用windows凭据。使用windows凭据时,用户必须在本地和远程PC上都有帐户。@jdweng我的客户端和服务器都在同一台PC上。为什么您认为这是一个证书例外?通常我会看到不同的错误,而您看到的是认证错误。我会首先使用像wireshark或fiddler这样的嗅探器来获取关于错误的更多信息。还要准确确定代码中失败的指令。您创建的证书是正确的SSL证书吗?此外,Chrome(至少)要求在证书中设置SAN,然后才能接受证书。你把它们放好了吗?另外,您是否只在受信任的根存储中或其他地方拥有证书?另外,在尝试HTTP客户机之前,请先检查其他客户机(其他浏览器等),确认一切正常。
Attackers might be trying to steal your information from *** (for example, passwords, messages, or credit cards). NET::ERR_CERT_AUTHORITY_INVALID
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;
}