Example #1
0
        private string WriteCertificateToDb_ReturnBase64Pfx(X509Certificate2 cert, string PfxPass)
        {
            //var cert = RequestClientCertificateFromPfxFile("client-auto-test", thumnprint, true, crlUri, hash, keySize, validDays);
            var certBytes = cert.Export(X509ContentType.Pfx, PfxPass); //export as pfx to test
            //System.IO.File.WriteAllBytes(fileName, certBytes);
            var base64Cert = System.Convert.ToBase64String(certBytes);
            IssuedCertificates newRecord = new IssuedCertificates();

            newRecord.CertificateThumbprint = cert.Thumbprint;
            newRecord.Base64Pfx             = base64Cert;
            newRecord.FriendlyName          = cert.FriendlyName;
            newRecord.Subject              = cert.Subject;
            newRecord.ValidTo              = cert.NotAfter;
            newRecord.ValidFrom            = cert.NotBefore;
            newRecord.SerialNumber         = cert.SerialNumber;
            newRecord.HasPk                = true;
            newRecord.PkSecret             = PfxPass; //bad practice right here
            newRecord.SignatureAlgorithmId = 1;
            newRecord.Version              = cert.Version;
            newRecord.IsRootCa             = false;
            newRecord.IsIntermediateCa     = false;
            newRecord.IsClientCert         = true;

            CertificateAuthorityContext db = new CertificateAuthorityContext();

            try
            {
                db.IssuedCertificates.AddAsync(newRecord);
            }
            catch
            { } //meh
            return(base64Cert);
        }
        public async Task <string> WriteCertificateToDb_ReturnBase64Pfx(X509Certificate2 cert, string PfxPass)
        {
            bool isRoot         = false;
            bool isIntermediate = false;
            bool isClient       = true;

            if (cert.Issuer == cert.Subject)
            {
                //root cert
                isRoot   = true;
                isClient = false;
            }


            var certBytes = cert.Export(X509ContentType.Pfx, PfxPass); //export as pfx to test
            //System.IO.File.WriteAllBytes(fileName, certBytes);
            var base64Cert = System.Convert.ToBase64String(certBytes);
            IssuedCertificates newRecord = new IssuedCertificates();

            newRecord.CertificateThumbprint = cert.Thumbprint;
            newRecord.Base64Pfx             = base64Cert;
            newRecord.FriendlyName          = cert.FriendlyName;
            newRecord.Subject              = cert.Subject;
            newRecord.ValidTo              = cert.NotAfter;
            newRecord.ValidFrom            = cert.NotBefore;
            newRecord.SerialNumber         = cert.SerialNumber;
            newRecord.HasPk                = true;
            newRecord.PkSecret             = PfxPass; //bad practice right here
            newRecord.SignatureAlgorithmId = 1;
            newRecord.Version              = cert.Version;
            newRecord.IsRootCa             = isRoot;
            newRecord.IsIntermediateCa     = isIntermediate;
            newRecord.IsClientCert         = isClient;

            CertificateAuthorityContext db = new CertificateAuthorityContext();

            try
            {
                await db.IssuedCertificates.AddAsync(newRecord);

                await db.SaveChangesAsync();
            }
            catch
            { } //meh
            return(base64Cert);
        }