Few hours ago, I thought it was an RSACryptoServiceProvider casting error.
But I attach the debuger to Azure AppService WebSite and I found out that the PrivateKey has an issue:
And if run the same certificate, but in localhost private key works well:
Exception Detail, when I tried to get certificate.PrivateKey property:
System.Security.Cryptography.CryptographicException: Invalid provider type specified.
at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
at System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle)
at System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair()
at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize)
at System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey()
And when I tried certificate.GetRSAPrivateKey() method:
System.Security.Cryptography.CryptographicException: Invalid provider type specified.
at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
at System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle)
at System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair()
at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize)
at System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey()
at System.Security.Cryptography.X509Certificates.RSACertificateExtensions.GetRSAPrivateKey(X509Certificate2 certificate)
The way I initialize the Certificate is this one:
public X509Certificate2 GetCertificate(byte[] pBytes,string pPassword)
{
X509Certificate2 vCertificate = null;
try
{
vCertificate = new X509Certificate2(pBytes,pPassword);
}
catch(Exception ex)
{
throw ex
}
return vCertificate
}
.Net Version Framework is v4.7
So should I need to configure something in the Azure configuration? Any clue?
Dont think this is a duplicate of: "Invalid provider type specified" CryptographicException when trying to load private key of certificate
It looks like is a Azure restriction, configuration or something like that.