![](/img/trans.png)
[英]The server mode SSL must use a certificate with the associated private key - during TLS handshake
[英]C# SSL server mode must use a certificate with the corresponding private key
我將學習如何在 C# 中作為服務器端處理 HTTPS 流量,至於第一步,我遇到了一些麻煩。
這是一些代碼( http://pastebin.com/C4ZYrS8Q ):
class Program
{
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;
}
static void Main()
{
var tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8080);
tcpListener.Start();
var clientAccept = tcpListener.AcceptTcpClient();
Thread.Sleep(1000);
if (clientAccept.Available > 0)
{
var sslStream = new SslStream(clientAccept.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
var certificate = new X509Certificate("path\server.pfx", "password");
sslStream.AuthenticateAsServer(certificate);
}
Console.ReadLine();
}
}
不要爭論! :) 它只是我只想在 C# 中使用 SSL 處理實現一些基本步驟的測試代碼。
所以......問題發生在這一行:
sslStream.AuthenticateAsServer(certificate);
從俄語它翻譯為:
我想,我的 X509 證書不正確,但再次檢查:
makecert.exe -r -pe -n "CN=localhost" -sky exchange -sv server.pvk server.cer
pvk2pfx -pvk server.pvk -spc server.cer -pfx server.pfx -pi <password>
似乎 X509 的創作一切都很好,其他證據表明這條線工作正常:
var certificate = new X509Certificate("path\server.pfx", "password");
並且程序沒有在上面的行上拋出異常。
那么,我的代碼中的 SSL 處理有什么問題,我如何將傳入的 SSL 流作為服務器端處理?
一切都很好,答案是使用X509Certificate2
類而不是X509Certificate
。
並將您創建的證書添加到信任列表中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.