简体   繁体   English

C# SSL 服务器模式必须使用带有相应私钥的证书

[英]C# SSL server mode must use a certificate with the corresponding private key

I'm going to learn how to handle HTTPS traffic in C# as server-side and as for the first steps I've got some troubles.我将学习如何在 C# 中作为服务器端处理 HTTPS 流量,至于第一步,我遇到了一些麻烦。

Here is some code ( http://pastebin.com/C4ZYrS8Q ):这是一些代码( 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();
    }
}

Don't argue!不要争论! :) It's the test code only where I just want to achieve some basic steps with the SSL handling in C#. :) 它只是我只想在 C# 中使用 SSL 处理实现一些基本步骤的测试代码。

So... The problem occurs at this line:所以......问题发生在这一行:

sslStream.AuthenticateAsServer(certificate);

在此处输入图片说明

From Russian it translates as:从俄语它翻译为:

  • SSL server mode must use a certificate with the corresponding private key. SSL 服务器模式必须使用带有相应私钥的证书。

I thought, that I've made my X509 certificate incorrect, but checked again:我想,我的 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>

And seems to be that all is fine with the X509 creation, and other proof is this line works fine:似乎 X509 的创作一切都很好,其他证据表明这条线工作正常:

var certificate = new X509Certificate("path\server.pfx", "password");

And program didn't throw an exception on the line above.并且程序没有在上面的行上抛出异常。

So, what's the problem with the SSL hanlding in my code and how can I handle incoming SSL stream as server-side?那么,我的代码中的 SSL 处理有什么问题,我如何将传入的 SSL 流作为服务器端处理?

All is fine, the answer is to use X509Certificate2 class instead of X509Certificate .一切都很好,答案是使用X509Certificate2类而不是X509Certificate

And to add to the trust list your created certificate.并将您创建的证书添加到信任列表中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 服务器模式 SSL 必须使用具有关联私钥的证书 - 在 TLS 握手期间 - The server mode SSL must use a certificate with the associated private key - during TLS handshake SslStream.AuthenticateAsServer 抛出服务器模式 SSL 必须使用具有关联私钥的证书 - SslStream.AuthenticateAsServer throws The server mode SSL must use a certificate with the associated private key SslStream.AuthenticateAsServer异常-服务器模式SSL必须使用带有关联私钥的证书 - SslStream.AuthenticateAsServer exception - The server mode SSL must use a certificate with the associated private key 获取“服务器模式 SSL 必须使用具有关联私钥的证书。” X509Certificate2 中的错误由 use.cer 而不是.pfx - Get "The server mode SSL must use a certificate with the associated private key." error in X509Certificate2 by use .cer instead of .pfx 为证书提供 EC 私钥以供在 HttpClient 中使用 C# - Providing an EC private key to certificate for use in HttpClient C# SSL证书和私钥(RSA) - SSL certificate and private key (RSA) 无法导入具有私钥的证书pfx从Windows Server 2012上的.net c#3.5存储 - Cannot import certificate pfx with private key to store from .net c# 3.5 on windows server 2012 如何在 C#/Xamarin 中使用带有来自 android 钥匙串的私钥的 X.509 证书? - How can I use an X.509 certificate with private key from the android keychain in C#/Xamarin? 将私钥添加到 X509Certificate2 - C# - Adding a private key to X509Certificate2 - C# 在 C# 中将公共证书与私有 RSA 密钥相结合 - Combining Public Certificate with Private RSA Key in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM