Pluralsight.Crypto.CryptContext.Open() throws System.IO.FileNotFoundException

1

When I want to create self-signed certificate using the method below:

public static byte[] CreateSelfSignCertificatePfx(string x500, DateTime startTime, DateTime endTime, SecureString password)
{
    X509Certificate2 cert;
    using (var ctx = new CryptContext())
    {
        ctx.Open();

        var certProps = new SelfSignedCertProperties();
        certProps.IsPrivateKeyExportable = true;
        certProps.KeyBitLength = 4096;
        certProps.Name = new X500DistinguishedName(x500);
        certProps.ValidFrom = startTime;
        certProps.ValidTo = endTime;

        cert = ctx.CreateSelfSignedCertificate(certProps);

        //X509Certificate2UI.DisplayCertificate(cert);
    }
    var result = cert.Export(X509ContentType.Pfx, password);
    return result;
}

It throws an exception...

System.IO.FileNotFoundException: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at Pluralsight.Crypto.Win32ErrorHelper.ThrowExceptionIfGetLastErrorIsNotZero()
   at Pluralsight.Crypto.CryptContext.Open()

Some tips?

c#
self-signed
pfx
asked on Stack Overflow Aug 31, 2016 by user6777300 • edited Sep 23, 2017 by Hans Kesting

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0