0

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.

prasanthi
  • 562
  • 1
  • 9
  • 25
  • Is the above is difficult or Didn't understood ? why no one is answering or replying to this question.Actually, I stuck with this exception.I am unable to find out the cause of this exception. Please help me out. – prasanthi Jul 13 '17 at 14:07
  • This doesn't answer your question directly; but can you download a JSON service credential instead of using the p12 format? With a JSON service credential you can use GoogleCredential.FromJson(...) which will sidestep any X509 issues. – Chris Jul 19 '17 at 15:26
  • Thank You @chris . when privatekey expries in JSON file? If privatekey expries what should I do? – prasanthi Jul 20 '17 at 08:44
  • As I understand it, private keys don't contain expiry information, so it can never expire. If you no longer want it, then the service credential can be revoked/deleted in the Google Cloud Console, meaning the credential will no longer provide access to your project. – Chris Jul 20 '17 at 10:35
  • Thank you @chris .when I export the private key in my machine .I was confused when i saw expire period is up to 10 years . whether that expire period is belongs to private key or project .If the that expire period is belongs to project then is there any other solution to prolong the expiration period. – prasanthi Jul 20 '17 at 14:20

1 Answers1

1

you can try to instantiate the X509Certificate with below code.

x509Certificate = new X509Certificate2(rawData, password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);

It works for me after investigating almost 2 days.