【问题标题】:Unable to cast object of type 'RSACng' to type 'System.Security.Cryptography.RSACryptoServiceProvider'无法将“RSACng”类型的对象转换为“System.Security.Cryptography.RSACryptoServiceProvider”类型
【发布时间】: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

【问题讨论】:

标签: c# oauth-2.0 google-authentication


【解决方案1】:

.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”类。

【讨论】:

    【解决方案2】:

    问题是应用程序在 .NET Core 环境中运行,但此特定代码位于 .NET Framework 的项目设置中。使用 .NET Core 设置修复问题将代码移动到项目。

    【讨论】:

    • @Yuri 您说问题是应用程序在 .NET 核心中运行,然后您将代码移动到 .NET 核心项目。迷茫了,已经有了。你想通了,所以请与社区分享。
    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多