为什么SslStream.AuthenticateAsServer从Ubuntu失败但从Windows 10失败

时间:2019-07-05 06:19:51

标签: c# ubuntu authentication .net-core sslstream

SslStream.AuthenticateAsServer方法在Ubuntu中失败,但在Windows 10中失败。

https://docs.microsoft.com/en-us/dotnet/api/system.net.security.sslstream?view=netframework-4.8实现SslStrem示例时 在Windows 10上执行时,它运行良好,但是在Ubuntu(Ubuntu)上执行时,调用sslStream.AuthenticateAsServer时出现以下异常:

  

System.NotSupportedException:服务器模式SSL必须使用   带有相关私钥的证书。

我尝试使用serverCertificate = X509Certificate2.CreateFromCertFile(certificate)代替serverCertificate = X509Certificate.CreateFromCertFile(certificate),但无济于事。

证书是使用OpenSSL生成的,执行模数检查时,所有密钥和证书实际上都匹配。服务器证书已使用CA证书签名。证书是使用jww'sHow do you sign a Certificate Signing Request with your Certification Authority?长答案

生成的

所以总结一下:在 Ubuntu 上(但在Windows 10上不是)调用sslStream.AuthenticateAsServer时,会引发以下异常:System.NotSupportedException:服务器模式SSL必须使用具有关联的私有证书键。

我正在使用.net-core环境(显然必须在Linux上运行):-)

它与建议的可能重复的X509Certificate2 the server mode SSL must use a certificate with the associated private key不同,因为它回答了证书的生成方式。但是,drake7707'sMark Yuan's的回答给了我一个提示。

因此,问题解决如下: 替换代码行

serverCertificate = X509Certificate.CreateFromCertFile(certificate);

使用

serverCertificate = new X509Certificate2(certificate);

并将其声明更改为

static X509Certificate serverCertificate = null;

static X509Certificate2 serverCertificate = null;

因此,总而言之,使用X509Certificate2确实可以解决问题。

1 个答案:

答案 0 :(得分:0)

因此,问题解决如下: 替换代码行

serverCertificate = X509Certificate.CreateFromCertFile(certificate);

使用

serverCertificate = new X509Certificate2(certificate);

并将其声明更改为

static X509Certificate serverCertificate = null;

static X509Certificate2 serverCertificate = null;

因此,总而言之,使用X509Certificate2确实可以解决问题。