Basicly everything is great if I run it in my machine in localhost, but when I tried to use it from Azure, I got this error:
System.Security.Cryptography.CryptographicException: Invalid provider type specified.
The code where this happen is this one:
X509Certificate2 certificate = new X509Certificate2(pBytes, "1234");
if (!certificate.HasPrivateKey)
{
//Here happens the Error
RSACryptoServiceProvider key = (RSACryptoServiceProvider)certificate.PrivateKey;
}
Any clue?
Btw, the project that Im using is https://github.com/ctt-gob-es/FirmaXadesNet.
And the entire code of that part is, maybe this will give a better background:
X509Certificate2 certificate = new X509Certificate2(pBytes, "1234");
var key = (RSACryptoServiceProvider)certificate.PrivateKey;
if (key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_STRONG_PROV ||
key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_ENHANCED_PROV ||
key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_DEF_PROV ||
key.CspKeyContainerInfo.ProviderName == CryptoConst.MS_DEF_RSA_SCHANNEL_PROV)
{
Type CspKeyContainerInfo_Type = typeof(CspKeyContainerInfo);
FieldInfo CspKeyContainerInfo_m_parameters = CspKeyContainerInfo_Type.GetField("m_parameters", BindingFlags.NonPublic | BindingFlags.Instance);
CspParameters parameters = (CspParameters)CspKeyContainerInfo_m_parameters.GetValue(key.CspKeyContainerInfo);
var cspparams = new CspParameters(CryptoConst.PROV_RSA_AES, CryptoConst.MS_ENH_RSA_AES_PROV, key.CspKeyContainerInfo.KeyContainerName);
cspparams.KeyNumber = parameters.KeyNumber;
cspparams.Flags = parameters.Flags;
_signingKey = new RSACryptoServiceProvider(cspparams);
_disposeCryptoProvider = true;
}
else
{
_signingKey = key;
_disposeCryptoProvider = false;
}