我已经开发了从我的应用程序访问谷歌电子表格。它可以正常工作几天但现在我遇到了一个例外
“System.Security.Cryptography.CryptographicException:指定的提供程序类型无效。”
我无法弄清楚问题是什么?我的代码在我的本地机器上运行良好,但在服务器中“出现异常”。
这是堆栈跟踪:
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.RSACryptoServiceProvider)certificate.PrivateKey).CspKeyContainerInfo.CryptoKeySecurity' threw an exception of type 'System.Security.AccessControl.PrivilegeNotHeldException'
在 X509certificate2 证书处看到了一个异常。因此,我认为由于此异常,我指定了无效的提供程序类型。
以下是有关我的证书的信息:
1. 文件 .p12
2. Providertype 是 1
3. 使用算法是sha1rsa。
4. 我的证书版本是v1。
如果您需要任何其他信息,请发表评论,我将提供。提前致谢。