我遇到了这个例外:
无法将类型为“ 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 apis libs 1.41.1版本
答案 0 :(得分:0)
问题在于该应用程序在.NET Core环境中运行,但是此特定代码在.NET Framework的项目设置中。将代码移动到具有.NET Core安装修复问题的项目中。
答案 1 :(得分:0)
.NET 核心 步骤 1:从 nuget pkg 安装 System.Security.Cryptography.Cng Step2:创建一个类“X509Certificate2Signature”
using iTextSharp.text.pdf.security;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace DigiSignNETCORE
{
public class X509Certificate2Signature : IExternalSignature
{
private String hashAlgorithm;
private String encryptionAlgorithm;
private X509Certificate2 certificate;
public X509Certificate2Signature(X509Certificate2 certificate, String hashAlgorithm)
{
if (!certificate.HasPrivateKey)
throw new ArgumentException("No private key.");
this.certificate = certificate;
this.hashAlgorithm = DigestAlgorithms.GetDigest(DigestAlgorithms.GetAllowedDigests(hashAlgorithm));
if (certificate.PrivateKey is RSACryptoServiceProvider)
encryptionAlgorithm = "RSA";
else if (certificate.PrivateKey is DSACryptoServiceProvider)
encryptionAlgorithm = "DSA";
else if (certificate.PrivateKey is System.Security.Cryptography.RSACng)
encryptionAlgorithm = "RSA";
}
public virtual byte[] Sign(byte[] message)
{
if (certificate.PrivateKey is RSACryptoServiceProvider)
{
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)certificate.PrivateKey;
return rsa.SignData(message, hashAlgorithm);
}
else if (certificate.PrivateKey is System.Security.Cryptography.RSACng)
{
System.Security.Cryptography.RSACng rSACng = (System.Security.Cryptography.RSACng)certificate.PrivateKey;
return rSACng.SignData(message,HashAlgorithmName.SHA1,RSASignaturePadding.Pkcs1);
}
else
{
DSACryptoServiceProvider dsa = (DSACryptoServiceProvider)certificate.PrivateKey;
return dsa.SignData(message);
}
}
public virtual String GetHashAlgorithm()
{
return hashAlgorithm;
}
public virtual String GetEncryptionAlgorithm()
{
return encryptionAlgorithm;
}
}
}
第 3 步:在这一行中 IExternalSignature externalSignature = new X509Certificate2Signature(cert, "SHA1");指向您最近创建的“X509Certificate2Signature”类。