Websocket fails to connect

Nivedita Parihar 21 Reputation points
2021-08-30T17:02:59.293+00:00

I have created websocket server and binded the self signed certificate with it , when websocket client fails to connect .
Eventviewer logs has this error.

"The TLS server credential's certificate does not have a private key information property attached to it. This most often occurs when a certificate is backed up incorrectly and then later restored. This message can also indicate a certificate enrollment failure."

Please find below code for creating self sign certificate.

var dn = new X500DistinguishedName("CN=" + Dns.GetHostName(),X500DistinguishedNameFlags.None);
var rsa = RSA.Create(); // generate asymmetric key pair
var req = new CertificateRequest(dn, rsa, HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1);
// key usage: Digital Signature and Key Encipherment
req.CertificateExtensions.Add(
new X509KeyUsageExtension(
System.Security.Cryptography.X509Certificates.X509KeyUsageFlags.KeyEncipherment,
true));
// Enhanced key usages
req.CertificateExtensions.Add(
new X509EnhancedKeyUsageExtension(
new OidCollection {
new Oid("1.3.6.1.5.5.7.3.1") // TLS Server auth
},
false));
// add this subject key identifier
req.CertificateExtensions.Add(
new X509SubjectKeyIdentifierExtension(req.PublicKey, false));

            cert = req.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now.AddYears(1));
            cert.FriendlyName = "Test";

            // Create PFX (PKCS #12) with private key
            string CertPath = Path.Combine(certPath, Guid.NewGuid() + ".pfx");
            File.WriteAllBytes(CertPath, cert.Export(X509ContentType.Pfx, ""));
Developer technologies .NET .NET Runtime
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.