X509Certificate Constructor Exception

后端 未结 9 1747
梦如初夏
梦如初夏 2020-11-27 15:38
//cert is an EF Entity and 
//    cert.CertificatePKCS12 is a byte[] with the certificate.

var certificate = new X509Certificate(cert.CertificatePKCS12, \"SomePassw         


        
相关标签:
9条回答
  • 2020-11-27 16:17

    The following code will help you, you can generate algorithm using bouncy castle library:

    private static ECDsa GetEllipticCurveAlgorithm(string privateKey)
    {
        var keyParams = (ECPrivateKeyParameters)PrivateKeyFactory
            .CreateKey(Convert.FromBase64String(privateKey));
    
        var normalizedECPoint = keyParams.Parameters.G.Multiply(keyParams.D).Normalize();
    
        return ECDsa.Create(new ECParameters
        {
            Curve = ECCurve.CreateFromValue(keyParams.PublicKeyParamSet.Id),
            D = keyParams.D.ToByteArrayUnsigned(),
            Q =
        {
            X = normalizedECPoint.XCoord.GetEncoded(),
            Y = normalizedECPoint.YCoord.GetEncoded()
        }
        });
    }
    

    and generate the token in the following way:

    var signatureAlgorithm = GetEllipticCurveAlgorithm(privateKey);
    
            ECDsaSecurityKey eCDsaSecurityKey = new ECDsaSecurityKey(signatureAlgorithm)
            {
                KeyId = settings.Apple.KeyId
            };
    
            var handler = new JwtSecurityTokenHandler();   
            var token = handler.CreateJwtSecurityToken(
                issuer: iss,
                audience: AUD,
                subject: new ClaimsIdentity(new List<Claim> { new Claim("sub", sub) }),
                expires: DateTime.UtcNow.AddMinutes(5), 
                issuedAt: DateTime.UtcNow,
                notBefore: DateTime.UtcNow,
                signingCredentials: new SigningCredentials(eCDsaSecurityKey, SecurityAlgorithms.EcdsaSha256));
    
    0 讨论(0)
  • 2020-11-27 16:21

    An alternative to changing the Load User Profile is to make the Application Pool use the Network Service Identity.

    See also What exactly happens when I set LoadUserProfile of IIS pool?

    0 讨论(0)
  • 2020-11-27 16:22

    To be able really solve your problem and not just guess, what can it be, one need be able to reproduce your problem. If you can't provide test PFX file which have the same problem you have to examine the problem yourself. The first important question is: are the origin of the exception "An internal error occurred" in the private key part of the PKCS12 or in the public part of the certificate itself?

    So I would recommend you to try to repeat the same experiment with the same certificate, exported without private key (like .CER file):

    var certificate = new X509Certificate(cert.CertificateCER);
    

    or

    var certificate = new X509Certificate.CreateFromCertFile("My.cer");
    

    It could help to verify whether the origin of your problem is the private key or some properties of the certificate.

    If you will have problem with the CER file you can safe post the link to the file because it have public information only. Alternatively you can at least execute

    CertUtil.exe -dump -v "My.cer"
    

    or

    CertUtil.exe -dump -v -privatekey -p SomePassword "My.pfx"
    

    (you can use some other options too) and post some parts of the output (for example properties of the private key without the PRIVATEKEYBLOB itself).

    0 讨论(0)
提交回复
热议问题