1

I am beginner to read the google spread using service account in my project .For few days it work fine but now i got an exception as

System.Security.Cryptography.CryptographicException: Invalid provider type specified.

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) 

By seeing this stack trace I came to know that it can't read the certificate.

here is my code:

string applicationName = "QA Automation";

       var serviceaccountemail = "Myaccount.gserviceaccount.com";

        var certificate = new X509Certificate2(GetCertificateBytes(), "*****", 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 = "**************************************";//spreadId

        String range = "Sheet1!A1:C";

        SpreadsheetsResource.ValuesResource.GetRequest request = 
            service.Spreadsheets.Values.Get(spreadsheetId, range);

        ValueRange response = request.Execute();

at .FromCeritificate(Certificate) got the exception. I have researched on this exception "Invalid provider type" .I got the results as it is permission of IIS . Is there any other solution to do this ?

Thanks in advance.

prasanthi
  • 562
  • 1
  • 9
  • 25
  • Firstly, it sounds like we can pretty much take the Google code out of the picture here - it's just calling the `PrivateKey` property. You could verify that by using `var privateKey = certificate.PrivateKey` just after `certificate.GetRSAPrivateKey()`. Having said that, looking at the code, I notice that in the netstandard1.3 assembly, we're using just `GetRSAPrivateKey()` - and it sounds like *that's* working, which makes this a little odd. If it's been working before, what's changed on your machine? Any updates? – Jon Skeet May 25 '17 at 06:07
  • Possible dupe of https://stackoverflow.com/questions/32430835? Beyond that, it may be something to do with the type of your certificate... – Jon Skeet May 25 '17 at 06:12
  • Thank for Reply -Jon Skeet, I have seen above question but my p12 file is attached to the project how I can change the IIS Permission in their systems who are accessing the project . – prasanthi May 25 '17 at 06:16
  • I'm not sure what you mean by that, I'm afraid. It sounds like you could do with giving us more context... – Jon Skeet May 25 '17 at 06:28
  • You have suggest that dupe of my question right !.In that question i have seen the answer but the answer is related to the IIS permission .I got this exception in the server how can i change the permision of the IIS . – prasanthi May 25 '17 at 06:34
  • Well doesn't that answer show you how to do so? It does feel like it would be better if we could avoid that call and just use GetPrivateRSAKey though... It might need a new target framework, is the downside :( – Jon Skeet May 25 '17 at 06:36
  • @Jon Skeet, i have tried by using GetPrivateRSAKey but I got an error at **.FromCeritificate(certificate.GetRSAPrivateKey()) ** as " cannot convert System.Security.Cryptography.RSA to System.Security.Cryptography.X509Certificates.X509Certificate2 " – prasanthi May 25 '17 at 06:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145094/discussion-between-prasanthi-and-jon-skeet). – prasanthi May 25 '17 at 06:59
  • A while ago I edited your code so that it was correctly indented and nicely readable - it would really help if you didn't undo that... – Jon Skeet May 25 '17 at 07:00
  • (Deleted my answer as it didn't come to anything.) To come back to a previous question: if it was working before, it sounds like something must have changed on your machine. I would suggest finding out what's changed, particularly in terms of IIS settings... – Jon Skeet May 25 '17 at 09:55
  • @JonSkeet I didn't getting what your trying . why your using RSACryptoServiceProvider.Is my question is related to any cng and crytposerviceprovider.I have researched .net framework 4.6 supports CNG than crytposerviceprovider – prasanthi May 25 '17 at 10:00
  • Our library targets .NET 4.5 and netstandard 1.3 though, so you're using the .NET 4.5 code path, which requires `RSACryptoServiceProvider`. It should still be available under .NET 4.6, for compatibility purposes. – Jon Skeet May 25 '17 at 10:01
  • @JonSkeet if the code is in server how can i change the IIS settings ? and my code is background process – prasanthi May 25 '17 at 10:02
  • @JonSkeet I am using .netFramework 4.6.1 then no need of using `RSACryptoServiceProvider` – prasanthi May 25 '17 at 10:08
  • You've missed my point - *you're* using .NET 4.6.1, but the *library* doesn't have a net46-specific target. But I don't understand why you wouldn't be able to change IIS settings just because the code is "in server" - of course it's in a server, in that anything using IIS is a server. By "in server" do you actually mean "in an environment I have no control over"? – Jon Skeet May 25 '17 at 10:32
  • Thanks you @JonSkeet. – prasanthi May 25 '17 at 11:01

0 Answers0