C# 无法强制转换类型为';RSACng&x27;输入';System.Security.Cryptography.rsacyptoserviceprovider';
我得到了一个例外: 无法将“RSACng”类型的对象强制转换为“System.Security.Cryptography.rsacCryptoServiceProvider”类型 调用此方法:C# 无法强制转换类型为';RSACng&x27;输入';System.Security.Cryptography.rsacyptoserviceprovider';,c#,oauth-2.0,google-authentication,C#,Oauth 2.0,Google Authentication,我得到了一个例外: 无法将“RSACng”类型的对象强制转换为“System.Security.Cryptography.rsacCryptoServiceProvider”类型 调用此方法: GoogleCredential cred = GoogleCredential.FromFile(path); 完全例外: Unable to cast object of type 'RSACng' to type 'System.Security.Cryptography.RSACryptoSer
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 libs版本1.41.1问题在于应用程序在.NET核心环境中运行,但此特定代码在.NET Framework的项目设置中。将代码移动到带有.NET Core安装程序的项目修复了此问题 .NET核心 步骤1:nuget pkg的insall System.Security.Cryptography.Cng 步骤2:创建类“X509Certificate2Signature”
步骤3:在此行中,IExternalSignature externalSignature=new X509Certificate2Signature(证书,“SHA1”);指向最近创建的类“X509Certificate2Signature”。查看此代码会尝试将RSA.Create()方法的结果强制转换为rsacryptServiceProvider。类RSA的工厂方法可以被machine.config覆盖,并且对于不同的.NET运行时,默认方法也可以不同,有关详细信息,请参见此处。可能没有人对此进行过深入测试:-)@Yuri你说问题在于应用程序运行在.NET core中,然后你将代码移动到了.NET core项目中。困惑,它已经在那里了。你明白了,所以请与社区分享。
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;
}
}
}