【发布时间】:2020-02-09 00:59:40
【问题描述】:
我遇到了这个异常:
无法将“RSACng”类型的对象转换为“System.Security.Cryptography.RSACryptoServiceProvider”类型
调用这个方法:
GoogleCredential cred = GoogleCredential.FromFile(path);
完全例外:
Unable to cast object of type 'RSACng' to type 'System.Security.Cryptography.RSACryptoServiceProvider'
at Google.Apis.Auth.OAuth2.ServiceAccountCredential.Initializer.FromPrivateKey(String privateKey) in C:\Apiary\2019-09-11.10-11-15\Src\Support\Google.Apis.Auth\OAuth2\ServiceAccountCredential.cs:line 110
at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.CreateServiceAccountCredentialFromParameters(JsonCredentialParameters credentialParameters) in C:\Apiary\2019-09-11.10-11-15\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:line 243
at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.CreateDefaultCredentialFromParameters(JsonCredentialParameters credentialParameters) in C:\Apiary\2019-09-11.10-11-15\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:line 197
at Google.Apis.Auth.OAuth2.GoogleCredential.FromFile(String path) in C:\Apiary\2019-09-11.10-11-15\Src\Support\Google.Apis.Auth\OAuth2\GoogleCredential.cs:line 114
at ServerUtil.GCloudReporter..ctor(String version, String deployEnv)
使用 .NET Framework 4.5.1
Google api 库版本 1.41.1
【问题讨论】:
-
查看github.com/googleapis/google-api-dotnet-client/blob/master/Src/… 此代码尝试将 RSA.Create() 方法的结果转换为 RSACryptoServiceProvider。类 RSA 的工厂方法可以被 machine.config 覆盖,并且对于不同的 .NET 运行时,默认值也可以不同,详情可以看这里dusted.codes/…。可能没有人对此进行过深入的测试:-)
标签: c# oauth-2.0 google-authentication