C# 如何解决System.Security.Cryptography.CryptographyException:指定的提供程序类型无效
我已经开发了从我的应用程序访问谷歌电子表格的功能。它可以正常工作几天,但现在我得到了一个例外 “System.Security.Cryptography.CryptographyException:提供程序无效 指定的类型。” 我想不出问题出在哪里? 我的代码在本地机器上运行良好,但在服务器上“获取异常” 以下是堆栈跟踪:C# 如何解决System.Security.Cryptography.CryptographyException:指定的提供程序类型无效,c#,google-sheets,cryptography,x509certificate2,system.security,C#,Google Sheets,Cryptography,X509certificate2,System.security,我已经开发了从我的应用程序访问谷歌电子表格的功能。它可以正常工作几天,但现在我得到了一个例外 “System.Security.Cryptography.CryptographyException:提供程序无效 指定的类型。” 我想不出问题出在哪里? 我的代码在本地机器上运行良好,但在服务器上“获取异常” 以下是堆栈跟踪: at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolea
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)
我已经搜索过了,但没有找到确切的答案。我认为我的代码没有问题
这是我的密码:
string applicationName = "#########";
var serviceaccountemail = "********************************";
var certificate = new X509Certificate2(GetCertificateBytes(), "notasecret", 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 = "*************************";
String range = "Sheet1!A1:C";
SpreadsheetsResource.ValuesResource.GetRequest request =
service.Spreadsheets.Values.Get(spreadsheetId, range);
ValueRange response = request.Execute();
public byte[] GetCertificateBytes()
{
return secure.SecureResource.qa_automation_google_key;
}
这里有一个更新:在调试上述代码时,我看到异常为
”((System.Security.Cryptography.rsacyptoServiceProvider)certificate.PrivateKey)。CspKeyContainerInfo.CryptoKeySecurity“引发了类型为“System.Security.AccessControl.privilegenetotheldexception”的异常。
atX509certificate2证书。
因此,我认为由于这个异常,我指定了一个无效的提供程序类型
以下是有关我的证书的信息:1.文件.p12
2.Providertype为1
3.使用算法是sha1rsa
4.我的证书版本为v1
如果您需要任何其他信息,请评论我将提供。
提前感谢。您可以尝试使用以下代码实例化X509证书 x509Certificate=新的X509Certificate2(原始数据、密码、X509keystrageFlags.MachineKeySet | X509keystrageFlags.PersistKeySet | X509keystrageFlags.Exportable)
经过近2天的调查后,我觉得这很有效。以上内容是困难的还是不理解的?为什么没有人回答这个问题。实际上,我坚持这个例外。我无法找出这个例外的原因。请帮帮我。这并不能直接回答你的问题;但是您可以下载JSON服务凭据而不是使用p12格式吗?使用JSON服务凭据,您可以使用GoogleCredential.FromJson(…),这将避免任何X509问题。谢谢@chris。privatekey何时在JSON文件中导出?如果privatekey expries我该怎么办?据我所知,私钥不包含过期信息,所以它永远不会过期。如果您不再需要它,则可以在Google云控制台中撤销/删除服务凭据,这意味着该凭据将不再提供对您的项目的访问。谢谢@chris。当我在我的机器中导出私钥时。当我看到过期期限长达10年时,我感到困惑。该过期期限是属于私钥还是属于project。如果该过期期限属于project,则是否有任何其他解决方案来延长过期期限。