I have developed accessing the google spreadsheet from my application .It works fine for few days but now i got an exception as
"System.Security.Cryptography.CryptographicException: Invalid provider type specified."
I am unable to figure it out what's the problem is? My code is working fine in my local machine but in the server "getting the exception".
Here is the stack trace :
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 Google.Apis.Auth.OAuth2.ServiceAccountCredential.Initializer.FromCertificate(X509Certificate2 certificate)
I have searched but i didn't find the exact solution.I think there is no problem with my code.
Here is my code:
string applicationName = "#########";
var serviceaccountemail = "********************************";
var certificate = new X509Certificate2(GetCertificateBytes(), "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceaccountemail)
{
Scopes = new[] { "https://spreadsheets.google.com/feeds" }
}.FromCertificate(certificate));
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = applicationName,
});
String spreadsheetId = "*************************";
String range = "Sheet1!A1:C";
SpreadsheetsResource.ValuesResource.GetRequest request =
service.Spreadsheets.Values.Get(spreadsheetId, range);
ValueRange response = request.Execute();
public byte[] GetCertificateBytes()
{
return secure.SecureResource.qa_automation_google_key;
}
Here is an update :
When i am debugging the above code i saw an exception as '((System.Security.Cryptography.RSACryptoServiceProvider)certificate.PrivateKey).CspKeyContainerInfo.CryptoKeySecurity' threw an exception of type 'System.Security.AccessControl.PrivilegeNotHeldException'
at X509certificate2 certificate.
So ,I thought becaue of this exception i got an invalid provider type specified.
Here is information about my certificate :
1. File .p12
2. Providertype is 1
3. using algorith is sha1rsa.
4. version of my certificate is v1.
If you want any other information please comment i will provide. Thanks in advance.